外部リンクを識別し、それらに「外部」クラスを追加するために、以下のコードを書きました。これを自分のサイトに実装しました。正常に動作していますが、「タブ」と「コメントに返信」オプションで正しく動作しない問題が 1 つあります。そこに「外部」クラスを追加していますが、それらはローカルリンクです。コードに問題がある場合はお知らせください。
タブのリンクは次のようになります。<a href="#tab2" class="external">Popular</a>
返信用のリンクは次のようになります。<a class="comment-reply-link external" href="/samsung-galaxy-ace-plus-s7500-how-to-root-and-install-custom-recovery-image/?replytocom=1044#respond" onclick="return addComment.moveForm("comment-1044", "1044", "respond", "420")">Reply</a>
これらは絶対リンクでlocation.host
はないため、これらのリンクでは機能しないため、失敗していることはわかっています。これらのリンクを組み込み、「ローカル」クラスを追加する方法を教えてください。
jQuery(document).ready(function ($) {
var root = new RegExp(location.host);
$('a').each(function(){
if(root.test($(this).attr('href'))){
$(this).addClass('local');
}
else{
// a link that does not contain the current host
var url = $(this).attr('href');
if(url.length > 1)
{
$(this).addClass('external');
}
}
});