Home > Database > Mysql Tutorial > body text

How to exclude the first element in jquery

WBOY
Release: 2022-04-02 10:49:18
Original
2917 people have browsed it

In jquery, you can use the not() method and the ":first" selector to exclude the first element. The not() method is used to return elements that do not meet certain conditions. The ":first" selector is used To select the first element, the syntax is "element object.not(":first")".

How to exclude the first element in jquery

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

How jquery excludes the first element

The not() method returns elements that do not meet certain conditions.

This method lets you specify a condition. Elements that do not meet the criteria will be returned from the selection, and elements that meet the criteria will be removed.

This method is usually used to remove one or more elements from the selected element combination.

:first selector selects the first element.

The example is as follows:

Create a new html file, named test.html, which is used in jquery to delete all li codes in ul except the first li. . Only after introducing the jquery.min.js library file and successfully loading the file can you use the methods in jquery. Use ul and li tags to create a list. The contents of li are test one, test two, and test three respectively. Use the button tag to create a button with the button name "Delete li". Bind the onclick click event to the button button. When the button is clicked, the delalli() function is executed.

Create a delalli() function. In the function, obtain the li object through "ul li", use the not() method to exclude the li that is not deleted, and use the ":first" in the not() method to exclude the first li object, use the remove() method to delete other li objects.

How to exclude the first element in jquery

Open the test.html file in the browser, click the button to see the effect.

How to exclude the first element in jquery

After clicking the button:

How to exclude the first element in jquery

Summary:

1. Create a test.html file.

2. In the file, use ul and li tags to create a list and create a button button to trigger the execution of the js function.

3. Create a function in the js tag. In the function, obtain the li object through "ul li", use the not() method to exclude li that is not deleted, and pass ":first" in the not() method. Exclude the first li object and use the remove() method to delete other li objects.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to exclude the first 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!