Home >Web Front-end >JS Tutorial >Several ways to delete/replace DOM elements with jQuery_jquery

Several ways to delete/replace DOM elements with jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:47:421084browse

Delete

The deletion operation is very simple. Just call the remove() method directly after the result set.

For example, to delete all a elements in the following html script, copy the code directly through

The code is as follows:

$('a'.remove();

That’s it.
Copy code The code is as follows:


Of course, you can also filter the selection results by passing parameters to remove, and then perform the remove operation
Copy code. The code is as follows:

$('a').remove('.remove');

It should be noted that

The remove operation will not delete the elements that meet the conditions from the result set, so in theory you can continue to operate the "deleted" elements
The remove operation will not only "delete" the element and the data related to all elements will also be Delete (event handlers, internally cached data)

Replace

If you want to replace the li element with class remove with
  • removed
  • , you can use the following two methods, etc. Price method
    Copy code The code is as follows:

    $('li.remove'). replaceWith('
  • removed
  • );
    $('
  • removed
  • ;).replaceAll('li.remove');
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn