Get the App
SLTechnology News&Howtos  ›  Development  › 

How jquery deletes the element itself

Shulou Source: shulou.com Published: 2022-05-31 18:35:50 09月20日 Update

Today, I would like to share with you how jquery deletes the relevant knowledge points of the element itself. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Delete method: 1, use remove () to delete the element and all its internal contents, the syntax is "specify the element .remove ();"; 2, use children () and unwrap () to delete the element, but keep the child nodes inside, and the syntax is "specify the element .children (). Remove ();".

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

Jquery deleting an element itself can be divided into two situations:

Delete the element itself and its contents (text and child nodes)

Delete only the element itself and keep the child nodes

Case 1. Use the remove () method

Use the remove () method to delete an element and everything inside it.

$(document) .ready (function () {$("button") .click (function () {$("div"). Remove ();}) Div {background-color: yellow;}

This is the paragraph in the div element.

This is the paragraph in the div element.

This is the paragraph in the div element.

Delete div element

Case 2: using the children () + unwrap () method

The children () method returns all direct child elements of the selected element.

The unwrap () method deletes the parent element of the selected element.

$(document) .ready (function () {$("button") .click (function () {$("div"). Children (). Unwrap ();})) Div {background-color: yellow;}

This is the paragraph in the div element.

This is the paragraph in the div element.

This is the paragraph in the div element.

Delete div element

The above is all the content of the article "how to delete the element itself by jquery". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Elements methods paragraphs this is content knowledge articles situations nodes grammar different big most that is tutorials text more versions environments computers. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Linux Shulou Tech Info Shulou Information MySQL