将类添加到<a>if href以开头http://www.twitter.com/.

add class to <a> if href starts with http://www.twitter.com/

本文关键字:http 开头 com twitter www if 添加 lt gt href      更新时间:2023-09-26

可能重复:
jQuery:选择<a>哪个href包含一些字符串

我正在为一个网站做一个小的twitter实现。我使用的javascript将代码导出如下:

<ul>
 <li>twitter message here <a href="http://twitter.com/username/ID">time of twitter</a></li>
</ul>

现在我喜欢在twitter链接中添加一个类,这样我就可以给它一个不同于tweets中普通链接的css。

有人想过如何以适当的方式解决这个问题吗?:(

$('a[href^="http://twitter.com"]').addClass('twitter');

http://jsfiddle.net/apjD6/

$(function() {
  $("a[href^='http://twitter.com/']").addClass("someClass");
});

请在此处查看参考资料:
http://api.jquery.com/attribute-starts-with-selector/