Get html value from object within an array

I have an exemplary JavaScript code snippet. What I'm trying to achieve here is getting html, and id attribute value from an object within the array

var swatches = $(".swatchColor");
for (var i = 0; i < swatches.length; i++) {
     var value = parseInt(swatches[i].html());
     if (!isNaN(value)) {
         alert(swatches[i].attr("id"));
     }
};

but for some reason I get Uncaught TypeError: undefined is not a function error when swatches[i].html() is executed. Why does it happen?


The jQuery class selector does not provide an array of node elements for you to iterate through.

From this answer, you need to do the following to iterate through all of the nodes:

$(".swatchColor").each(function(i, obj) {
    var value = parseInt($(obj).html());
    //etc...
});
链接地址: http://www.djcxy.com/p/83526.html

上一篇: 如何一次获取ap元素的内容

下一篇: 从数组中的对象获取html值