Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How jquery calculates how many elements are under an element

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how jquery calculates the number of elements under an element". In daily operation, I believe many people have doubts about how jquery calculates how many elements are under an element. The editor consulted all kinds of data and sorted out a simple and useful method of operation. I hope it will be helpful to answer the question of "how to calculate the number of elements under an element by jquery". Next, please follow the editor to study!

Calculation method: 1, use find () to get all the elements under the specified element (including a subset of the subset) subset elements, syntax "specify element object .find (filter)", will return a collection of elements; 2, use the length attribute to get the number of elements contained in the set of elements, syntax "element collection .length".

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

When jquery calculates the number of elements under an element, it calculates the number of all subset elements under that element (including a subset of the subset).

Realize the idea:

Using the find () method to get all subset elements returns a collection of elements

Use the length attribute to get the length of the element collection, that is, the number of elements contained in the element collection

Implementation code:

.div, div * {display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px } $(document) .ready (function () {$("button") .on ("click") Function () {$("ul") .find ("*") .css ({"color": "red", "border": "2px solid red"}) Var len=$ ("ul") .find ("*") .length; console.log ("ul element has" + len+ "element");});}) Div (parent node) ul (specified element) li (child node 1) span (grandnode 1) Li (Child Node 2) span (grandNode 2) li (Child Node 3) span (grandNode 3) Select all child elements of ul And calculate the number.

We can see whether it is right to output six.

Let's check it out: 3 li child elements + 3 span grandchild elements = 6 Magi OK is right!

At this point, the study on "how jquery calculates how many elements are under an element" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report