我确实发现了几个关于 NOT_FOUND_ERR: DOM Exception 8
的问题与 jQuery 结合使用时出现错误,但它们并没有发生在像我这样的场景中,因此它们没有提供解决方案。
基本上,我有一个对象,我正在迭代它,然后将行添加到 <table>
与 id="legend"
: http://jsfiddle.net/nt9gZ/ .
var items = [],
obj = {a: 1,
b: 2};
$.each(obj, function(i, v) {
items.push(
$("<tr>").append(
$("<td>").html(i),
$("<td>").html(v)
)
);
});
// .empty() is to erase contents when running this piece of code again
$("#legend").empty().append(
$(items)
);
当我运行这段代码时,出现错误:
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
在 Chrome 上。
我不确定我的代码到底出了什么问题。
- 找不到什么?
- 我该如何解决这个问题?
最佳答案
你正试图插入一个数组的数组..
在将创建的对象推送到数组中时添加 .get(0)
,以便插入实际的 DOM 片段。
$.each(obj, function(i, v) {
items.push(
$("<tr>").append(
$("<td>").html(i),
$("<td>").html(v)
).get(0)
);
});
关于javascript - 使用 jQuery 追加到表时出现 NOT_FOUND_ERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6889934/