JavaScript 错误:使用 jQuery 和数组的 DOM 异常 8
我收到此错误:
未捕获的错误:NOT_FOUND_ERR:DOM 例外8
这是我的代码(请提出任何建议以使其更高效/更干净):
基本上,这是一个按钮,将其 ID 添加到名为“keywords”的数组中:
$('.add').live('click', function() {
if($(this).text() == "+Add") {
console.log("add triggered");
$(this).stop().animate({backgroundColor:'#999d92'}, 300);
$(this).html("-Rem").fadeIn('fast');
keywords.push($(this).attr("id"));
$("#response").append(keywords);
}
else {
$(this).stop().animate({backgroundColor:'#cc6633'}, 300);
$(this).html("+Add").fadeIn('fast');
var index = keywords.indexOf($(this).attr("id"));
keywords.splice(index, index+1);
$("#response").append(keywords);
}
});
我想要发生的是当按下“+ADD”时id 属性被添加到数组中,当按下 -REM 时,然后从关键字中删除该 id。
任何建议都会很有帮助。当我将 $(this).attr("id") 附加到响应 div 时,它确实可以正确打印。我还尝试用“String()”函数包围它(也许它是对资源的引用,而不是实际的字符串?)
谢谢!
I am getting this error:
Uncaught Error: NOT_FOUND_ERR: DOM
Exception 8
Here is my code (please suggest anything to make it more efficient / cleaner):
Basically this is a button thats adding it's ID to an array called 'keywords':
$('.add').live('click', function() {
if($(this).text() == "+Add") {
console.log("add triggered");
$(this).stop().animate({backgroundColor:'#999d92'}, 300);
$(this).html("-Rem").fadeIn('fast');
keywords.push($(this).attr("id"));
$("#response").append(keywords);
}
else {
$(this).stop().animate({backgroundColor:'#cc6633'}, 300);
$(this).html("+Add").fadeIn('fast');
var index = keywords.indexOf($(this).attr("id"));
keywords.splice(index, index+1);
$("#response").append(keywords);
}
});
What I want to happen is when "+ADD" is pushed the id attribute is added to the array, and when -REM is pushed then remove that id from the keywords.
Any advice would really be a help. When I just append $(this).attr("id") to the response div it does print correctly. I also tried surrounding it with a "String()" function (perhaps it was a reference to the resource and not the actual string?)
Thanks a head of time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
splice
的第二个参数是要删除的项目数,因此可能应该只是1
。但是
keywords
是一个字符串数组? jQuery 文档中没有这样的append
接口。你想要类似的东西吗:?
The second argument to
splice
is a number of items to remove, so that should probably be just1
.But
keywords
is an array of strings? jQuery documents no such interface forappend
.Do you want something like:?