Uncaught TypeError: Object # has no method 'attr'

后端 未结 2 1746
南方客
南方客 2021-01-03 23:49

Im trying to grab my divs with the class tooltip.

And then do something like this:

var schemes = $(\".tooltip\");

for (var i in schemes) {
    var s         


        
相关标签:
2条回答
  • 2021-01-04 00:17
    var schemes = $(".tooltip");
    
    schemes.each(function(index, elem) {
        console.log($(elem).attr('cost'));
    });
    

    As a sidenote "cost" is not a valid attribute for any element as far as I know, and you should probably be using data attributes.

    0 讨论(0)
  • 2021-01-04 00:31

    If you use for-loop to iterate jQuery set, you should get the elements with eq() method, but not using square bracket notation (i.e. []). The code like $(".tooltip")[i] will pick up DOM elements, but not jQuery objects.

    var schemes = $(".tooltip");
    for (var i = 0; i < schemes.length; i++) {
        var scheme = schemes.eq(i);
        console.log(scheme.attr("cost"));
    }
    

    However, you may always use each() to iterate jQuery set:

    $(".tooltip").each(function() {
        var scheme = $(this);
        console.log(scheme.attr("cost"));
    });
    
    0 讨论(0)
提交回复
热议问题