如果我有這樣一串鏈接所有鏈接:添加類鏈接到某個域與JS(jQuery的)
<a href="foo.com">blah</a> and this <a href="example.com">one</a> and here is another <a href="foo.com"></a>.
我怎麼會一類添加到所有鏈接到FOO鏈接.COM?
如果我有這樣一串鏈接所有鏈接:添加類鏈接到某個域與JS(jQuery的)
<a href="foo.com">blah</a> and this <a href="example.com">one</a> and here is another <a href="foo.com"></a>.
我怎麼會一類添加到所有鏈接到FOO鏈接.COM?
,以確保你得到http://foo.com,http://bar.foo.com/about,而不是http://bazfoo.com,嘗試:
$("a[href*='/foo.com'], a[href*='.foo.com']").addClass('your_class');
這裏的正則表達式更強的解決方案,這可能是比較慢,你要知道,但檢查所訪問的開始:
$("a").filter(
function(){
return $(this).attr('href')
.match(/^https?:\/\/([^/]*\.)?foo\.com(\/.*|$)/i);
})
.addClass('your_class');
這裏有一些測試用例:http://jsbin.com/oruhu
(你可以在這裏編輯:http://jsbin.com/oruhu/edit)。
$("a[href='foo.com']").addClass('your_class')
中平凡: $("a[href='http://foo.com']").addClass('foo');
但是,假定該URL完全匹配。
如果你有聯繫的像foo的域不同的網頁:
<a href="http://foo.com/eggs.html">
<a href="http://foo.com/bacon.html">
那麼你可以使用這樣的選擇:
$("a[href^=http://foo.com/]").addClass("newClass")
它會發現,與「http://foo.com/啓動所有鏈接「 或
$("a[href*=/foo.com/]").addClass("newClass")
這將找到所有鏈接包含「/foo.com/」
您的代碼在addClass之前缺少'.'。 – Mottie 2009-11-11 07:41:21
固定。謝謝。 – 2009-11-11 14:22:15