javascript - 无法删除 DOM

标签 javascript jquery

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/

相关文章:

javascript - 在 safari mobile 上播放(和重播)声音

jquery - PhoneGap 1.5.0 Android Cordova AJAX JSON

jquery - 为什么我在 Django 中收到 403 POST 请求错误?

javascript - HTML 元素添加到 Javascript for 循环中,然后在计数器递增时删除

javascript - 动态禁用 SVG 元素的触摸操作(过度滚动)

javascript - 如何从数组和已存在的对象创建新对象?

javascript - 在 Javascript/JQuery 中访问/修改 Ruby 类属性

javascript - 打印我的网页时,我可以将标题 'attached' 保留在我的内容中吗?

javascript - 如何在另一个列表的点击事件上添加类

php - 使用 CSV 文件的 JavaScript 和 JQuery 多维数组搜索