jquery学习 - jquery选择孩子元素和个数/获取css属性

来源:互联网 发布:如何淘宝购物 编辑:程序博客网 时间:2024/05/21 21:34

  • 选择器
  • 选择孩子元素和css属性
  • 获得孩子元素的个数

选择器

jquery的选择器很强大,可以的话,能用jquery的时候,真的是非常方便。

选择孩子元素和css属性

先看下面的代码:

SelectColor = $(ColorId).children('svg').children('rect').css('fill');

这个代码很容易懂。首先:

ColorId = $(“#id”);
svg是一个子元素的标签。
rect是svg下的子元素
fill是rect的一个css属性

可以看到我用了两个children, 是因为jquery里每次只能选择一层的子元素,不能越层级。

获得孩子元素的个数

其实这个也很简单。
例如我们有下面代码:

<div class="box" id="c0" onclick="changeColor('c0', '#fe0000')">                <svg width="32" height="32" viewPort="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">             <rect x="6" y="6" width="20" height="20" style="fill:#fe0000;"></rect>        <polyline fill="none" stroke="black" class="polylinehide" style="stroke-width:2" points="2,16 2,30 16,30 "/></polyline>        <polyline fill="none" stroke="black" class="polylinehide" style="stroke-width:2" points="16,2 30,2 30,16 "/></polyline>    </svg></div>

这个是例子的html代码。我们想获取id = c0的下面的polyline的个数:(应该为2) 代码如下:

var polyNum = $('#co').children('svg').children('polyline');alert(polyNum.length);

这个就是获取个数的了。

0 0