Jquery learning-jquery selects child elements and numbers/gets css attributes
Select the child element and css attribute to obtain the number of child elements.
Selector
Jquery's selector is very powerful. If you can, it is very convenient to use jquery.
Select child element and css attribute
Let's take a look at the following code:
SelectColor = $(ColorId).children('svg').children('rect').css('fill');
This code is easy to understand. First:
ColorId = $ ("# id ");
Svg is a sub-element label.
Rect is a child element under svg.
Fill is a css attribute of rect.
We can see that I used two children, because jquery can only select one layer of child elements at a time and cannot be more hierarchical.
Obtain the number of child elements
In fact, this is also very simple.
For example, we have the following code:
This is the html code of the example. We want to obtainid = c0
(It should be 2) the code is as follows:
var polyNum = $('#co').children('svg').children('polyline');alert(polyNum.length);
This is the number.