DOM内にアンカーがあり、次のコードではこれをファンシーボタンに置き換えます。これはうまくいきますが、もっとたくさんのボタンが必要な場合はクラッシュします。 for-loopなしでそれを行うことはできますか?いくつかの要素の問題を伴うJQuery
$(document).ready(buttonize);
function buttonize(){
//alert(buttonAmount);
//Lookup for the classes
var button = $('a.makeabutton');
var buttonContent = button.text();
var buttonStyle = button.attr('class');
var link = button.attr('href');
var linkTarget = button.attr('target');
var toSearchFor = 'makeabutton';
var toReplaceWith = 'buttonize';
var searchButtonStyle = buttonStyle.search(toSearchFor);
if (searchButtonStyle != -1) {
//When class 'makeabutton' is found in string, build the new classname
newButtonStyle = buttonStyle.replace(toSearchFor, toReplaceWith);
button.replaceWith('<span class="'+newButtonStyle
+'"><span class="left"></span><span class="body">'
+buttonContent+'</span><span class="right"></span></span>');
$('.buttonize').click(function(e){
if (linkTarget == '_blank') {
window.open(link);
}
else window.location = link;
});
}
}
どのようにクラッシュしますか? –
'$( 'a.makeabutton')'、要素のコレクションを返す 'button.each(機能を...)を使用し、' – Ibu
DOMは次のようになります。クラスは色のある \t
\t \t Buttonized! \t \t
\t \t Buttonized! \t \t
\t \t Buttonized! \t \t
\t \t \t