JQuery有几个Elements问题

时间:2011-10-26 19:27:24

标签: javascript jquery html

我在DOM中有一个锚点,下面的代码用一个花哨的按钮替换它。这很好,但如果我想要更多的按钮,它会崩溃。我可以在没有for循环的情况下完成吗?

$(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; 
        });
    }
}

2 个答案:

答案 0 :(得分:0)

var button              =   $('a.makeabutton');

此代码返回一个包含所有匹配锚点的jQuery对象。您需要使用.each

循环遍历它们
$(document).ready(buttonize);

function buttonize() {
    //alert(buttonAmount);
    //Lookup for the classes
    var $buttons = $('a.makeabutton');
    $buttons.each(function() {
        var button = $(this);
        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) {

            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;
            }); // end click
        } // end if

    }); // end each
}

答案 1 :(得分:0)

使用each方法,因为您正在获取元素集合(即使它只是一个)

var button              =   $('a.makeabutton');
button.each(function () {
    var btn = $(this);
    var buttonContent       =   btn.text();
    var buttonStyle         =   btn.attr('class');
    var link                =   btn.attr('href');
    var linkTarget          =   btn.attr('target');
    var toSearchFor         =   'makeabutton';
    var toReplaceWith       =   'buttonize';
    var searchButtonStyle   =   buttonStyle.search(toSearchFor);
    ...
};

每个方法循环遍历检索到的所有元素,您可以使用this关键字来引用循环中的当前元素