gpt4 book ai didi

javascript - 无法删除 DOM

转载 作者:行者123 更新时间:2023-11-28 11:52:43 25 4
gpt4 key购买 nike

enter image description here

上图是我正在做的。当点击任何一个空方 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com