jquery学习 - jquery选择孩子元素和个数/获取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);

这个就是获取个数的了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值