简体   繁体   English

jQuery的几个要素问题

[英]JQuery with several Elements issue

i got an anchor in the DOM and the following code replaces it with a fancy button. 我在DOM中找到了一个锚点,下面的代码将其替换为一个精美的按钮。 This works well but if i want more buttons it crashes. 这很好用,但是如果我想要更多按钮,它会崩溃。 Can I do it without a for-loop? 我可以不使用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; 
        });
    }
}
var button              =   $('a.makeabutton');

This code returns a jQuery object which contains all the matching anchors. 这段代码返回一个jQuery对象,其中包含所有匹配的锚点。 You need to loop through them using .each : 您需要使用.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
}

Use the each method because you are fetching a collection of elements (even if its just one) 使用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);
    ...
};

the each method loops through all the elements that were retrieved, and you can use the this keyword to refer to the current element in the loop each方法循环遍历所有检索到的元素,您可以使用this关键字引用循环中的当前元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM