简体   繁体   English

带有Regex的CSS2属性选择器

[英]CSS2 Attribute Selectors with Regex

CSS Attribute selectors allow the selection of elements based on attribute values. CSS属性选择器允许基于属性值选择元素。 Unfortunately, I've not used them in years (mainly because they're not supported by all modern browsers). 不幸的是,我多年没有使用它们(主要是因为它们不受所有现代浏览器的支持)。 However, I remember distinctly that I was able to use them to adorn all external links with an icon, by using a code similar to the following: 但是,我清楚地记得,通过使用类似于以下的代码,我能够使用它们来装饰带有图标的所有外部链接:

a[href=http] {
    background: url(external-uri);
    padding-left: 12px;
}

The above code doesn't work. 上面的代码不起作用。 My question is: How does it work? 我的问题是: 如何工作的? How do I select all <a> tags whose href attribute starts with "http" ? 如何选择href属性以"http"开头的所有<a>标签? The official CSS spec (linked above) doesn't even mention that this is possible. 官方CSS规范(上面链接)甚至没有提到这是可能的。 But I do remember doing this. 但我确实记得这样做。

( Note : The obvious solution would be to use class attributes for distinction. I want to avoid this because I have little influence of the way the HTML code is built. All I can edit is the CSS code.) 注意 :显而易见的解决方案是使用class属性进行区分。我想避免这种情况,因为我对HTML代码的构建方式几乎没有影响。我可以编辑的只是CSS代码。)

As for CSS 2.1, see http://www.w3.org/TR/CSS21/selector.html#attribute-selectors 至于CSS 2.1,请参阅http://www.w3.org/TR/CSS21/selector.html#attribute-selectors

Executive summary: 执行摘要:

Attribute selectors may match in four ways:

    [att]
    Match when the element sets the "att" attribute, whatever the value of the attribute.
    [att=val]
    Match when the element's "att" attribute value is exactly "val".
    [att~=val]
    Match when the element's "att" attribute value is a space-separated list of
    "words", one of which is exactly "val". If this selector is used, the words in the 
    value must not contain spaces (since they are separated by spaces).
    [att|=val]
    Match when the element's "att" attribute value is a hyphen-separated list of
    "words", beginning with "val". The match always starts at the beginning of the
    attribute value. This is primarily intended to allow language subcode matches
    (e.g., the "lang" attribute in HTML) as described in RFC 3066 ([RFC3066]).

CSS3 also defines a list of selectors , but the compatibility varies hugely . CSS3还定义了一个选择器列表 ,但兼容性差异很大

There's also a nifty test suite that that shows which selectors work in your browser. 还有一个漂亮的测试套件 ,可以显示哪些选择器在您的浏览器中工作。

As for your example, 至于你的例子,

a[href^=http]
{
    background: url(external-uri);
    padding-left: 12px;
}

should do the trick. 应该做的伎俩。 Unfortunately, it is not supported by IE. 不幸的是,IE不支持它。

Antti's answer is sufficient for selecting anchor's whose href's begin with http and gives a perfect rundown on the available CSS2 regex-esque attribute selectors, like so: Antti的答案足以选择其href以http开头的锚点,并在可用的CSS2 正则表达式属性选择器上提供完美的纲要,如下所示:

 Attribute selectors may match in four ways: [att] Match when the element sets the "att" attribute, whatever the value of the attribute. [att=val] Match when the element's "att" attribute value is exactly "val". [att~=val] Match when the element's "att" attribute value is a space-separated list of "words", one of which is exactly "val". If this selector is used, the words in the value must not contain spaces (since they are separated by spaces). [att|=val] Match when the element's "att" attribute value is a hyphen-separated list of "words", beginning with "val". The match always starts at the beginning of the attribute value. This is primarily intended to allow language subcode matches (eg, the "lang" attribute in HTML) as described in RFC 3066 ([RFC3066]). 

However, here is the appropriate, UPDATED way to select all outgoing links using the new CSS3 :not pseudo class selector as well as the new *= substring syntax to make sure it disregards any internal links that may still begin with http : 但是,这是使用新的CSS3选择所有传出链接的适当的UPDATED方法:非伪类选择器以及新的* = substring语法 ,以确保它忽略可能仍以http开头的任何内部链接:

a[href^=http]:not([href*="yourdomain.com"])
{
    background: url(external-uri);
    padding-left: 12px;
}

*Note that this is unsupported by IE, up to at least IE8. *请注意,这不受IE支持,至少IE8。 Thanks, IE, you're the best :P 谢谢,IE,你是最好的:P

Note that, in Antti's example you'd probably want to add a catch for any absolute links you may have to your own domain, which you probably don't want to flag as 'external', eg: 请注意,在Antti的示例中,您可能希望为您可能拥有的任何绝对链接添加一个catch,您可能希望将其标记为“外部”,例如:

a[href^="http://your.domain.com"]
{
    background: none;
    padding: 0;
}

And you'd want this after the previous declaration. 在上次声明之后你会想要这个。

You might also want to include the full protocol prefix, just in case you have a local document named "http-info.html" that you wish to link to, eg: 您可能还希望包含完整的协议前缀,以防您希望链接到名为“http-info.html”的本地文档,例如:

a[href^="http://"]
{
    background: url(external-uri);
    padding-left: 12px;
}

Note that, in both these slightly-more complex cases, you should quote the value. 请注意,在这些稍微复杂的情况下,您应引用该值。 These work, for me, in IE7. 对我来说,这些工作在IE7中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM