Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery:如何删除包含所有内容的标记_Jquery_Html - Fatal编程技术网

jquery:如何删除包含所有内容的标记

jquery:如何删除包含所有内容的标记,jquery,html,Jquery,Html,当单击第二个带有remove class的标记时,我想删除整个部分 HTML: 但它只删除第二个内部div,该div包含带有remove class的a标记。使用classtr获取最近的div元素 $("body").on("click", ".remove", function (e) { if (x > 1) { $(this).closest('div.tr').remove(); // ^^ x

当单击第二个带有remove class的标记时,我想删除整个部分

HTML:

但它只删除第二个内部div,该div包含带有remove class的a标记。

使用class
tr
获取最近的
div
元素

$("body").on("click", ".remove", function (e) {
    if (x > 1) {
        $(this).closest('div.tr').remove();
        //                 ^^
        x--;
    }
.最近的()

对于集合中的每个元素,通过测试元素本身并在DOM树中遍历其祖先,获取与选择器匹配的第一个元素

使用:

要删除整行,而不仅仅是div。

使用

$(this).parents(".tr").remove(); 


这对答案有什么影响?@Muhammad Osmond,它保留了我添加然后删除的元素数量!不管怎样,正如Barmar提到的,这里没有任何区别……只是想知道,他为什么在click函数上使用if语句?
$("body").on("click", ".remove", function (e) {
    if (x > 1) {
        $(this).closest('div.tr').remove();
        //                 ^^
        x--;
    }
$(this).closest("tr").remove();
$(this).parents(".tr").remove(); 
$(this).closest(".tr").remove();