jQueryを使ってtargetの属性値を取得してアイコンを変更する
久々にjQueryを扱うと、いろいろと戸惑いますね。
■ソースコード:jQuery$(function() { $('a').each(function(){ var linktype = $(this).attr('target'); if(linktype == "_blank") { $(this).addClass("blank"); } else if(linktype == "_new") { $(this).addClass("new"); } else { $(this).addClass("link"); } }); });
jQueryの主な流れ
1)ページ内に存在する複数のaタグを個別に処理する為に.eachを使用
2)aのtarget属性の属性値を.attrで取得
3)取得した属性値を元に該当するclassを追加(if文)