如何删除动态添加的HTML代码块

huangapple go评论72阅读模式
英文:

How to delete dynamically added html code block

问题

I have a button which dynamically creates html content enclosed in <fieldset> like this.:

<div id="box_parent"></div>

Javascript

var content = 
    '<fieldset>Some Content <input id="delete_row_box" type="button" value="Delete"></fieldset>';
$("#box_parent").append(content);

How can I make the dynamically created delete button only delete the HTML block that it belongs to?

I'm stuck on this function:

$(document).on("click", '#delete_row_box', function() {
    console.log(this);
});

I think .remove() should do it but I don't know how to throw the correct reference.

英文:

I have a button which dynamically creates html content enclosed in &lt;fieldset&gt; like this.:

&lt;div id=&quot;box_parent&quot;&gt;&lt;/div&gt;

Javascript

var content = 
&#39;&lt;fieldset&gt;Some Content &lt;input id=&quot;delete_row_box&quot; type=&quot;button&quot; value=&quot;Delete&quot;&gt;&lt;/fieldset&gt;&#39;;
$(&quot;#box_parent&quot;).append(content);

How can I make the dynamically created delete button only deletes the html block that it only belongs to?

I'm stuck on this function:

$(document).on(&quot;click&quot;, &#39;#delete_row_box&#39;, function() {
    console.log(this);
});

I think .remove() should do it but I don't know how to throw the correct reference

答案1

得分: 2

Sure, here is the translated content:

Whole Content

如果您想要使用jQuery删除父容器中的整个内容,请使用以下代码:

$(document).on("click", '#delete_row_box', function() {
    $("#box_parent").children().remove();
});

One Row

不要为可重复标签使用ID。在JS中无法正常工作。只需像我在示例中使用的适当类名。

如果您想要附加特定的框以进行删除操作,那么您应该在附加时为子项指定ID,或者应该将删除按钮放置在每个框内,类似于每个项目的关闭按钮。

如果行的容器是按钮的直接父级

使用以下代码查找要移除的父级:

$(document).on("click", '.delete_row_box', function() {
    $(this).parent().remove();
});

如果行的容器是按钮的爷爷级

使用以下代码查找要移除的行:

$(document).on("click", '.delete_row_box', function() {
    $(this).closest('fieldset').remove();
});
  • 您可以将fieldset替换为与您的父级匹配的选择器。
英文:

Whole Content

If you want to delete the whole content in box parent using jQuery, you should use this code:

$(document).on(&quot;click&quot;, &#39;#delete_row_box&#39;, function() {
    $(&quot;#box_parent&quot;).children().remove();
});

One Row

Do not use ID for repeatable tags. It's not working in JS. Just use a suitable class name as I used in examples

If you want a specific box appended to delete, then you should either give an id to the child during append or you should place the delete button inside the each box like a close button for each item.

If row's container is direct parent of the button

Find the parent to remove using this code:

$(document).on(&quot;click&quot;, &#39;.delete_row_box&#39;, function() {
    $(this).parent().remove();
});

If row's container is a grand parent of the button

Find the row you need to remove using this code:

$(document).on(&quot;click&quot;, &#39;.delete_row_box&#39;, function() {
    $(this).closest(&#39;fieldset&#39;).remove();
});
  • you can replace fieldset with the selector that matches your parent.

答案2

得分: 0

选择其父级,然后删除您添加的 fieldset

$(this).parent().remove()
英文:

Select its parent then delete the fieldset you add

$(this).parent().remove()

答案3

得分: 0

尝试添加一个id并移除那个id。
这段代码应该能正常工作,它只会删除"一些内容",不会移除你创建的删除按钮。如果你需要生成多个内容,你可以在创建内容时添加一个索引,然后使用for循环或while循环来选择要删除的内容(带有多个删除按钮)。你甚至可以使用类而不是id来一次性删除它们所有。

var content = '<fieldset><span id="content">一些内容</span> <input id="delete_row_box" type="button" value="删除"></fieldset>';
$("#box_parent").append(content);

$(document).on("click", '#delete_row_box', function() {
    $("#content").remove();
});
英文:

try to add an id and remove that id.
This code should work fine and it will only delete "Some content", it will not remove the delete button you created. If you need to generate multiple content you can add an index when you create the content and a for or while loop to select the content to remove (with multiple delete button). You can even use a class instead of an id to delete them all in one go.

var content = &#39;&lt;fieldset&gt;&lt;span id=&quot;content&quot;&gt;Some Content&lt;/span&gt; &lt;input 
id=&quot;delete_row_box&quot; type=&quot;button&quot; value=&quot;Delete&quot;&gt;&lt;/fieldset&gt;&#39;;
$(&quot;#box_parent&quot;).append(content);

$(document).on(&quot;click&quot;, &#39;#delete_row_box&#39;, function() {
    $(&quot;#content&quot;).remove();
});

答案4

得分: 0

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

    var content = '&#39;&lt;fieldset&gt;一些内容 &lt;input id=&quot;delete_row_box&quot; type=&quot;button&quot; value=&quot;删除&quot;&gt;&lt;/fieldset&gt;&#39;';
    $("#box_parent").append(content);

    $(document).on("click", '#delete_row_box', function() {
        $(this).closest('fieldset').remove();
    });

&lt;!-- 语言: lang-html --&gt;

    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;div id="box_parent"&gt;&lt;/div&gt;


&lt;!-- 结束代码片段 --&gt;
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var content = &#39;&lt;fieldset&gt;Some Content &lt;input id=&quot;delete_row_box&quot; type=&quot;button&quot; value=&quot;Delete&quot;&gt;&lt;/fieldset&gt;&#39;;
$(&quot;#box_parent&quot;).append(content);

$(document).on(&quot;click&quot;, &#39;#delete_row_box&#39;, function() {
    $(this).closest(&#39;fieldset&#39;).remove();
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;box_parent&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月1日 16:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定