Home > Article > Web Front-end > How to delete the first node element in jquery
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();".
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.
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.
Open the test.html file in the browser to check the effect.
After clicking the button:
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!