我在写剧本
<div>包装这些子数组。我有这样的工作:
var elems = $('p');
content = [];
temp = [];
for(var i = 0; i < elems.length; i++) {
var elem = elems[i];
temp.push(elem);
if (i === 2 || i+1 === elems.length) {
content.push(temp);
temp = [];
}
};
console.log(content);
$.each(content, function(i) {
var elem = $(this);
elem.wrapAll($('<div class="wrapper"></div>'));
});看这把小提琴 --它使用一个包含元素的对象
但是,当我返回jQuery对象而不是仅仅返回元素时,wrapAll()函数突然中断,抛出一个TypeError:Value does not implement interface Node.,而其他函数(替换wrapAll)不会中断(比如wrap()或wrapInner())。
代码:
var elems = $('p');
content = [];
temp = [];
for(var i = 0; i < elems.length; i++) {
var elem = $(elems[i]);
temp.push(elem);
if (i === 2 || i+1 === elems.length) {
content.push(temp);
temp = [];
}
};
console.log(content);
$.each(content, function(i) {
$(this).wrapAll($('<div class="wrapper"></div>'));
});参见这个小屁孩 (小提琴抛出另一个TypeError: context是未定义的,但我认为这是由于jsfiddle处理框架的方式所致)。
我是不是走错路了?但是wrapAll不应该起作用吗?(在第二个Fiddle中用work()替换它,这样就可以了)
发布于 2013-05-24 13:46:13
试试这个:
var elems = $('p').get();
content = [];
temp = [];
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
temp.push(elem);
if (i === 2 || i + 1 === elems.length) {
content.push(temp);
temp = [];
}
};
console.log(content);
$.each(content, function (i) {
$(this).wrapAll($('<div class="wrapper"></div>'));
});小提琴演示
另外,您还可以看到以下内容:使用切片演示
https://stackoverflow.com/questions/16736354
复制相似问题