Home > Web Front-end > JS Tutorial > How to delete the first node element in jquery

How to delete the first node element in jquery

WBOY
Release: 2021-11-30 11:58:03
Original
3245 people have browsed it

In jquery, you can use the eq() and remove() methods to delete the first node element. The eq() method is used to select the first node element, and the remove() method is used to delete the selected node element. Node, the syntax is "$(selector).eq(0).remove();".

How to delete the first node element in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

How to delete the first node of an element in jquery

In jquery, use eq(0) to get the first node, and pass remove( ) method to implement deletion.

The example is as follows:

1. Create a new html file, named test.html, to explain how jquery deletes the first child element. Create a module using a div, and within the div, use the p tag to create four lines of text. Set the id of the div tag to testdiv, which is mainly used to obtain the div object through the id below.

Use the button tag to create a button with the button name "Delete the first child element". Bind the onclick click event to the button button. When the button is clicked, the myfun() function is executed.

How to delete the first node element in jquery

2. In the js tag, create the myfun() function. Within the function, obtain the div object through id(testdiv) and obtain all child elements through the children() method. , use eq(0) to obtain the first child element, and delete it through the remove() method.

How to delete the first node element in jquery

Open the test.html file in the browser to check the effect.

How to delete the first node element in jquery

After clicking the button:

How to delete the first node element in jquery

Summary:

1. Create a test.html file .

2. In the file, use div to create a module. Within the div, use the p tag to create four lines of text. At the same time, create a button button to trigger the execution of the js function.

3. In the js tag, create a function. Within the function, obtain the div object, obtain all child elements through the children() method, use eq(0) to obtain the first child element, and obtain the first child element through remove( ) method to implement deletion.

Note:

eq(0) represents the first sub-element, eq(1) represents the second sub-element, and so on.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to delete the first node element in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template