简体   繁体   English

如何从Javascript中删除HTML中的元素?

[英]How to remove elements in HTML from Javascript?

I have a javascript function that appends elements into my HTML page. 我有一个javascript函数,可将元素追加到HTML页面中。 I want to add a function that when a user clicks a button in HTML it will remove the associated HTML content from the page. 我想添加一个函数,当用户单击HTML中的按钮时,它将从页面中删除关联的HTML内容。

$("#keywords").append("<td><img src=\"images/delete.png\" class=\"delete_filter\" /> " + id + "</td>");

The code above is how I am adding elements to the HTML. 上面的代码是我向HTML添加元素的方式。 So in this case when a user clicks the appended image <td><img src=\\"images/delete.png\\" class=\\"delete_filter\\" /> will be removed from the HTML. 因此,在这种情况下,当用户单击附加的图像<td><img src=\\"images/delete.png\\" class=\\"delete_filter\\" />时,将从HTML中删除。

This is what I have now that isn't working: 这是我现在无法正常工作的内容:

$(".delete_filter").click(
function(){
    ($(this).parent()).remove();
});

Because the element is being dynamically added click() will not work as it only sees elements which were available on load. 因为该元素是动态添加的,所以click()不会起作用,因为它只能看到加载时可用的元素。

Try this instead: 尝试以下方法:

$("#myTable").delegate(".delete_filter", "click", function() {
    $(this).parent().remove();
})

Or in jQuery 1.7+ 或在jQuery 1.7+中

$("#myTable").on("click", ".delete_filter", function() {
    $(this).parent().remove();
})

The problem appears to be that you're adding the click handler before the DOM element is added to the page. 问题似乎是您在将DOM元素添加到页面之前添加了click处理程序。 Hence it's not binding to that element. 因此,它不绑定到该元素。 Try using on instead 尝试改用on

$('table').on('click', '.delete-filter', function() {
  $(this).parent().remove();
});

The on API is new to jQuery 1.7. on API是jQuery 1.7的新功能。 If you're using an earlier version then try live 如果您使用的是早期版本,请尝试live

$('table').delegate('.delete-filter', 'click', function() {
  $(this).parent().remove();
});

The elements won't be part of the html at the point you set up your event listeners, try the following: 在设置事件侦听器时,这些元素将不属于html,请尝试以下操作:

$('table').delegate('.delete_filter', 'click', function() {
  $(this).parent().remove();
});

Try: 尝试:

$('#keywords').on('click', '.delete_filter', function() {
    $(this).parent().remove();

});

Probably your code doesn't work because the delete_filter class objects aren't there yet when the code is executed. 您的代码可能无法正常工作,因为执行代码时delete_filter类对象尚不存在。 The on syntax binds it dynamic so it should work.. on语法将其动态绑定,因此它应该可以工作。

Try this 尝试这个

$(document).ready(function(){

    $("#keywords").append("<td><img src=\"images/delete.png\" class=\"delete_filter\" /> "  + id + "</td>");

    $(".delete_filter").on("click", function(event) {

     // Not sure why you had this section wrapped in a function() tag, wasn't necessary
     $(this).parent().remove();

    });
});

Try this: Bind the event after appending the item to DOM. 尝试以下操作:将项目附加到DOM 之后,绑定事件。

$("#keywords").append("<td><img src=\"images/delete.png\" class=\"delete_filter\" /> " + id + "</td>");
$(".delete_filter").click(function() {
    $(this).parent().remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM