上图是我正在做的。当点击任何一个空方 block 时,它应该将值附加到 $("#captureAvail")
和$("#captureAvail2")
.同时它必须是一个使正方形颜色变为绿色的类。该类称为“green2”。现在,当再次单击该项目或删除“green2”类时,我无法成功删除该元素。请帮我解决这个问题。下面是我的代码。当我添加这一行时:$("#captureAvail").remove("<input type='hidden' name='avail[]' value='"+thisValue+"'>");
,我发现方 block 变得不可点击。下面是我的脚本。
$("#greeny td:not(:first-child)").on("click",function()//prevent the first column to be clickable
{
//alert($(this).text());
var thisValue = $(this).text();
//
if($(this).hasClass('green2'))
{
$(this).removeClass('green2');
$("#captureAvail").remove("<input type='hidden' name='avail[]' value='"+thisValue+"'>");
$("#captureAvail2").remove("<input type='hidden' name='avail[]' value='"+thisValue+"'>");
}else
{
$(this).addClass('green2');
$("#captureAvail").append("<input type='hidden' name='avail[]' value='"+thisValue+"'>");
$("#captureAvail2").append("<input type='hidden' name='avail[]' value='"+thisValue+"'>");
}
//$(this).toggleClass("green2");
//
//$("#captureAvail").css("background-color","#ff0000");
});
最佳答案
remove
函数删除 jquery 选择器中匹配的元素。您需要删除它的子级:
$("#captureAvail").empty();
关于javascript - 无法删除 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712795/