2011-12-13 8 views

答えて

3

我々はページ(<a>要素)上のリンクの話をしている場合は、この方法を試してください。

// regular expression to check for domain.com 
// won't match tricky ones like domain.company.com nodomain.com :) 
var re = /(^|\.)domain\.com(\/|$)/i; 

// loop through all the links 
$("a").each(function() { 
    // test if href attribute belongs to domain.com 
    if(re.test($(this).attr('href'))) { 
    // do what you want with links that belong to domain.com 
    $(this).addClass("domaincom"); 
    } 
}); 

HEREがコードです。

+1

実際、あなたのコードは完全に正しいものではありません。ピリオドをエスケープするには '.com'が' \ .com'でなければなりません。それ以外の場合は、 "完全一致"正規表現として扱われます。 – mc10

+0

@ mc10 Ooops、あなたは140%右です:)。私は自分の答えを更新しました。ありがとう! – kubetz

関連する問題