我想将几个div的属性(文本长度和样式)存储到数组中。然后我想根据div文本长度将样式键的值放在其他div中。

$("#blocks .block span").each(function() {
     blockspansizes.push({length: $(this).text().length, style:$(this).attr("style")});
});//text lengths are all different.


$(".newblocks span").each(function() {
         textlength = (this).text().length
 //if textlength matches one length value from array, get its corresponding style from the same object.
    });

最佳答案

您不能像这样推送到数组,然后比较值。您可以使用键/值对来做到这一点:

$("#blocks .block span").each(function() {
   blockspansizes[$(this).text().length]=$(this).attr("style");
 });//text lengths are all different.


$(".newblocks span").each(function() {
     textlength = (this).text().length;
      //if textlength matches one length value from array, get its corresponding style from  the      same object.
     if(blockspansizes.indexOf(textlength) > -1){
        //do something with blockspansizes[textlength]
     }

});

08-19 13:29