//外部リンクblank設定
$('#hoge').find('a[href^=http]').not('[href*="'+location.hostname+'"]').not('[href*="mailto"]').attr({target:"_blank"});
指定した範囲の中でhttpから始まる外部リンクがあった場合に、aタグにtarget blankを追加するサンプルコードです。
空白のタブが開く事になるのでメーラーリンクは除外しています。
classも追加する場合
//外部リンクblank設定 blankというclassも追加する
$('#hoge').find('a[href^=http]').not('[href*="'+location.hostname+'"]').not('[href*="mailto"]').addClass('blank').attr({target:"_blank"});
classも追加する場合はaddClassを追記してやります。
サイト全体に適応する場合
//外部リンクblank設定
$('a[href^=http]').not('[href*="'+location.hostname+'"]').not('[href*="mailto"]').attr({target:"_blank"});
関連リンク
- jQuery – CSSのclassを追加・削除する『 addClass() / removeClass() / toggleClass() 』
- 要素の属性を操作するには?(attr/removeAttr)
- 【jQuery】複数の要素に対して繰り返し処理をする(each・for)