IE7 / 8的jQuery map()错误 [英] jQuery map() bug with IE7/8
问题描述
我有以下jquery代码:
I have the following jquery code:
$('.menubox').children('div').toArray().map(function(n,i){
return [$(n).children('input').val(),$(n).children('.subs').children('div').toArray().map(function(n,i){
return [$(n).children('input').val(),$(n).children('.subs').children('div').toArray().map(function(n,i){
return $(n).children('input').val();
})];
})];
});
这是与之相关的HTML:
This is the HTML to go with that:
<td class="menubox">
<div class="draggable droppable lvl0">
<input type="hidden" value="-" />
LBL1
<div class="subs"></div>
</div>
<div class="draggable droppable lvl0">
<input type="hidden" value="-" />
LBL2
<div class="subs">
<div class="draggable droppable lvl1">
<input type="hidden" value="3" />
<b>LBL2.1</b><span style="display:block;"><a class="deleteItem">verwijder</a></span>
<div class="subs"></div>
</div>
<div class="draggable droppable lvl1">
<input type="hidden" value="6" />
<b>LBL2.2</b><span style="display:block;"><a class="deleteItem">verwijder</a></span>
<div class="subs"></div>
</div>
</div>
</div>
</td>
它在Chrome中工作正常但是IE抛出错误:
It works fine in chrome but IE throws an error:
错误:对象不支持此属性或方法
Error: Object doesn't support this property or method
当我删除map()函数,错误消失了。我在这做什么wronge?
When I remove the map() function, the error is gone. What am I doing wronge here?
推荐答案
我在这做什么冤枉?
What am I doing wronge here?
在jQuery对象上调用 .toArray()
。删除这些电话,并在 <$ c后添加 .get()
$ c> .map() 如果你想得到一个vanilla JS数组。请注意,jQuery的 .map()
回调需要 index,元素
而 Array.map
的回调需要元素,索引
所以你还需要交换参数名。
Calling .toArray()
on a jQuery object. Remove those calls, and add .get()
after .map()
if you want to end up with a vanilla JS array. Note that jQuery's .map()
callback takes index, element
while Array.map
's callback takes element, index
so you also need to swap the argument names.
var results = $('.menubox').children('div').map(function(i, n)
{
return [
$(n).children('input').val(),
$(n).children('.subs').children('div').map(function(i, n)
{
return [
$(n).children('input').val(),
$(n).children('.subs').children('div').map(function(i, n)
{
return $(n).children('input').val();
}).get()
];
}).get()
];
}).get();
console.log(JSON.stringify(results));
// ["-",[],"-",["3",[],"6",[]]]
这适用于Chrome但不支持IE,因为Chrome支持 Array.map()
但是(惊讶,惊讶)IE< 9不要。
This works in Chrome but not IE since Chrome supports Array.map()
but (surprise, surprise) IE <9 do not.
这篇关于IE7 / 8的jQuery map()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!