Javascript 如何设置特定标记的样式

Javascript 如何设置特定标记的样式,javascript,jquery,html,css,styling,Javascript,Jquery,Html,Css,Styling,我有很多这样的链接: <a href="http://www.youtube.com/user/nigahiga">Youtube</a> <a href="http://pinterest.com/pin/41236152807615600/">Pinterest</a> <a href="https://www.facebook.com/manchesterunited">Facebook</a> <a href=

我有很多这样的链接:

<a href="http://www.youtube.com/user/nigahiga">Youtube</a>
<a href="http://pinterest.com/pin/41236152807615600/">Pinterest</a>
<a href="https://www.facebook.com/manchesterunited">Facebook</a>
<a href="http://www.youtube.com/user/RayWilliamJohnson">Youtube</a> 
<a href="http://pinterest.com/pin/24910604158520050/">Pinterest</a>
但它不起作用。如何实现它?

您可以使用:

或者最好只需要纯css:

a[href*="pinterest"] {  
    color: DeepSkyBlue;
} 

您可以使用下一个选择器获取链接到pinterest的所有超链接:

$('a[href^="http://pinterest.com/"]')
该选择器将返回“href”属性以开头的所有“a”元素


然后,您可以使用属性包含解决方案(虽然没有那么整洁)来设置它们的样式,也可以使用以下功能:

$('a').filter(function () {
    return $(this).attr("href").indexOf("pinterest") != -1;
}).css('color', 'red');

选择器文档可在以下位置找到:


您应该检查每个a的href attr值

$('a')。每个(函数(){

如果($(this.attr('href')。包含('pinterest'))

$(this.css)(您的样式)


}))

您是否在控制台中看到了错误消息并阅读了该错误消息不起作用的原因?contains()不适用于attr返回的字符串。如果您使用indexof、match或test之类的工具,您可以不受影响。没错,您可以在…css中使用更多内容({color:''aaf',background:'444',margin:'20px'))
$('a[href^="http://pinterest.com/"]')
$('a').filter(function () {
    return $(this).attr("href").indexOf("pinterest") != -1;
}).css('color', 'red');
  $('a[href*="pinterest"]').css('apply styles');
For attributes:

= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains