我想做的是循环播放此HTML并获取我要获取的HTML值的嵌套数组.
起初看起来可能很复杂
但这是一个简单的问题…
html
<div class="configureData">
<div title="Large">
<a href="yellow" title="true" rel="$55.00" name="sku22828"></a>
<a href="green" title="true" rel="$55.00" name="sku224438"></a>
<a href="Blue" title="true" rel="$55.00" name="sku22222"></a>
</div>
<div title="Medium">
<a href="yellow" title="true" rel="$55.00" name="sku22828"></a>
<a href="green" title="true" rel="$55.00" name="sku224438"></a>
<a href="Blue" title="true" rel="$55.00" name="sku22222"></a>
</div>
<div title="Small">
<a href="yellow" title="true" rel="$55.00" name="sku22828"></a>
<a href="green" title="true" rel="$55.00" name="sku224438"></a>
<a href="Blue" title="true" rel="$55.00" name="sku22222"></a>
</div>
</div>
javascript
//This script is just part of a Object containing methods.
parseData:function(dH){
dH.find(".configureData div").each(function(indA,eleA){
colorNSize.tempSizeArray[indA] = [eleA.title,[],[]]
$(eleZ).find("a").each(function(indB,eleB){
colorNSize.tempSizeArray[indA][indB] = eleB.title
})
})
},
我希望最终数组应如下所示.
[
["large",["yellow","green","blue"],["true","true","true"],["$55","$55","$55"]
],["Medium","$55"]
]
]
// and so on....
最佳答案
给定您的HTML和此jQuery代码段:
原文链接:https://www.f2er.com/jquery/531008.htmlvar result = [];
$('.configureData div').each(function () {
var $a = $('a',this);
result.push([this.title,$.map(['href','title','rel'],function (a) {
return [$.map($a,function (v) {
return $(v).attr(a)
})];
})
]);
});
您可以按照自己的方式设置结果.
如果您有Firebug,只需执行console.dir(result)即可查看.