样式元素在类名中带有点(.)

dot*_*tty 61 css class

干我有这样的元素

<span class='a.b'>
Run Code Online (Sandbox Code Playgroud)

不幸的是,这个类名来自电子商务应用程序,无法更改.

我可以设置带有点的类名称吗?

喜欢

.a.b { }
Run Code Online (Sandbox Code Playgroud)

RoT*_*oRa 96

.a\.b { }
Run Code Online (Sandbox Code Playgroud)

然而,可能有浏览器不支持这一点.

  • 我不确定(因此"可能").然而,IE6令人惊讶. (2认同)
  • 您需要使用反斜杠转义属于类名称一部分的点,因此在这种情况下:`span.a\.b`.示例:http://jsfiddle.net/Mrafq/1/ (2认同)

Mat*_*aic 36

这个派对来得很晚,但你可以使用属性选择器.

在您的情况下,要定位class='a.b'元素,您可以使用:

[class~="a.b"] {...}
// or
span[class~="a.b"] {...}
Run Code Online (Sandbox Code Playgroud)

此外,这是属性选择器的完整列表.

属性当前选择器

// Selects an element if the given attribute is present

// HTML
<a target="_blank">...</a>

// CSS
a[target] {...}
Run Code Online (Sandbox Code Playgroud)

属性等于选择器

// Selects an element if the given attribute value
// exactly matches the value stated

// HTML
<a href="http://google.com/">...</a>

// CSS
a[href="http://google.com/"] {...}
Run Code Online (Sandbox Code Playgroud)

属性包含选择器

// Selects an element if the given attribute value
// contains at least once instance of the value stated

// HTML
<a href="/login.php">...</a>

// CSS
a[href*="login"] {...}
Run Code Online (Sandbox Code Playgroud)

属性从选择器开始

// Selects an element if the given attribute value
// begins with the value stated

// HTML
<a href="https://chase.com/">...</a>

// CSS
a[href^="https://"] {...}
Run Code Online (Sandbox Code Playgroud)

属性结束于选择器

// Selects an element if the given attribute value
// ends with the value stated

// HTML
<a href="/docs/menu.pdf">...</a>

// CSS
a[href$=".pdf"] {...}
Run Code Online (Sandbox Code Playgroud)

属性间隔选择器

// Selects an element if the given attribute value
// is whitespace-separated with one word being exactly as stated

// HTML
<a href="#" rel="tag nofollow">...</a>

// CSS
a[rel~="tag"] {...}
Run Code Online (Sandbox Code Playgroud)

属性连字符选择器

// Selects an element if the given attribute value is
// hyphen-separated and begins with the word stated

// HTML
<a href="#" lang="en-US">...</a>

// CSS
a[lang|="en"] {...}
Run Code Online (Sandbox Code Playgroud)

资料来源:learn.shayhowe.com

  • 属性间隔选择器可能更适合于一般情况,因为可能存在您不想选择的元素上的其他类别. (3认同)