2

我收到此错误:

未捕获的错误:NOT_FOUND_ERR:DOM 异常 8

这是我的代码(请提出任何建议以使其更高效/更清洁):

基本上,这是一个将其 ID 添加到名为“关键字”的数组中的按钮:

$('.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()”函数包围它(也许它是对资源的引用,而不是实际的字符串?)

感谢时间的负责人!

4

1 回答 1

4
keywords.splice(index, index+1);

to 的第二个参数splice是要删除的项目的数量,所以它可能应该只是1.

$("#response").append(keywords); 

但是keywords是一个字符串数组吗?jQuery 文档没有针对append.

你想要类似的东西:?

$("#response").text(keywords.join(', '));
于 2011-06-02T22:44:29.407 回答