加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 业界 > 正文

jQuery-Selectors(选择器)的行使(六、属性篇)

发布时间:2018-08-19 15:38:35 所属栏目:业界 来源:站长网
导读:关于火狐, IE8 在赏识此文章时会有一些错误(选择器失灵)征象的声明: 起首,感激各人对本系列文章的存眷及提出的BUG。 我写文章时,用的是IE7的情形,没有思量到火狐和其余赏识器,这是我的疏忽,在往后的文章中,我会作赏识器测试。经我测试,Radio和Ch
关于火狐,IE8在赏识此文章时会有一些错误(选择器失灵)征象的声明:
起首,感激各人对本系列文章的存眷及提出的BUG。
我写文章时,用的是IE7的情形,没有思量到火狐和其余赏识器,这是我的疏忽,在往后的文章中,我会作赏识器测试。经我测试,Radio和CheckBox在火狐赏识器下改变配景致是浮现不出来的,在IE8下亦是云云,添加边框也是浮现不出来的,如$("input").css("border","red 2px solid");是没有结果的。但jQuery选择器选择的功效是正确的,假如您用$("input").attr("checked","true");这句代码即可测出。
这篇文章我一偶然刻就会做出修改,找一些火狐、IE等赏识器都支持的结果来作为实例。 1. [attribute]用法
界说:匹配包括给定属性的元素
返回值:Array<Element>
参数:attribute (String) : 属性名
实例:将ID为"div_a1"的DIV中有ID属性的span元素的配景致改为赤色
代码: $("#div_a1 span[id]").css("background-color","red"); //点击按钮一将执行这句代码 DIV ID="div_a1"
span ID="span_1"
span 无ID属性
span ID="span_2"
DIV ID="div_a5" 2. [attribute=value]用法
界说:匹配给定的属性是某个特定值的元素
返回值:Array<Element>
参数:attribute (String):属性名 value (String):属性值。引号在大大都环境下是可选的。但在碰着诸如属性值包括"]"时,用以停止斗嘴。
实例:将ID为"div_b1"的DIV中name属性值为chk_attribute_test的input元素的配景致改为赤色
代码:$("#div_b1 input[name=chk_attribute_test]").css("background-color","red"); //点击按钮二将执行这句代码 DIV ID="div_b1"

radio name='rd'
radio name='rd'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
DIV ID="div_b5" 3. [attribute!=value]用法
界说:匹配给定的属性是不包括某个特定值的元素
返回值:Array<Element>
参数:attribute (String):属性名 value (String):属性值。引号在大大都环境下是可选的。但在碰着诸如属性值包括"]"时,用以停止斗嘴。
实例:将ID为"div_c1"的DIV中name属性值不是chk_attribute_test的input元素的配景致改为赤色
代码:$("#div_c1 > input[name!=chk_attribute_test]").css("background-color","red"); //点击按钮三将执行这句代码 DIV ID="div_c1"

radio name='rd'
radio name='rd'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
DIV ID="div_c5" 留意:这里我用了'>',假如将'>'换成' ',则按钮三的配景颜色也会酿成赤色 4. [attribute^=value]用法
界说:匹配给定的属性是以某些值开始的元素
返回值:Array<Element>
参数:attribute (String):属性名 value (String):属性值。引号在大大都环境下是可选的。但在碰着诸如属性值包括"]"时,用以停止斗嘴。
实例:将ID为"div_d1"的DIV中name属性值以'txt'开头的input元素的配景致改为赤色
代码:$("#div_d1 > input[name^=txt]").css("background-color","red"); //点击按钮四将执行这句代码 DIV ID="div_d1"

radio name='rd'
radio name='rd'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'

DIV ID="div_d5" 5. [attribute$=value]用法
界说:匹配给定的属性是以某些值末了的元素
返回值:Array<Element>
参数:attribute (String):属性名 value (String):属性值。引号在大大都环境下是可选的。但在碰着诸如属性值包括"]"时,用以停止斗嘴。
实例:将ID为"div_e1"的DIV中name属性值以'list'末了的input元素的配景致改为赤色
代码:$("#div_e1 > input[name$=list]").css("background-color","red"); //点击按钮五将执行这句代码 DIV ID="div_e1"
checkbox name='chk_attribute_list'

radio name='rd'
radio name='rd'
checkbox name='chk_attribute_list'
checkbox name='chk_attribute_list'
checkbox name='chk_attribute_list'

checkbox name='chk_attribute_list'
DIV ID="div_e5" 6. [attribute*=value]用法
界说:匹配给定的属性是以包括某些值的元素
返回值:Array<Element>
参数:attribute (String):属性名 value (String):属性值。引号在大大都环境下是可选的。但在碰着诸如属性值包括"]"时,用以停止斗嘴。
实例:将ID为"div_f1"的DIV中name属性值包括'_'的input元素的配景致改为赤色
代码:$("#div_f1 > input[name*=_]").css("background-color","red"); //点击按钮六将执行这句代码 DIV ID="div_f1"

radio name='rd'
radio name='rd'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'
checkbox name='chk_attribute_test'

DIV ID="div_f5" 7. [selector1][selector2][selectorN]用法
界说:复合属性选择器,必要同时满意多个前提时行使。
返回值:Array<Element>
参数:selector1 (Selector):属性选择器 selector2 (Selector):另一个属性选择器,用以进一步缩小范畴 selectorN (Selector):恣意多个属性选择器
实例:将ID为"div_g1"的DIV中有id属性且name属性值以'rd'开头和以'test'末了的input元素的配景致改为赤色
代码:$("#div_g1 > input[id][name$=test][name^=rd]").css("background-color","red"); //点击按钮七将执行这句代码 DIV ID="div_g1"

radio id='rd_0' name='rd_test'
radio id='rd_1' name='rd_test'
checkbox id='chk_0' name='chk_attribute_test'
checkbox id='chk_1' name='chk_attribute_test'
checkbox id='chk_2' name='chk_attribute_test'
checkbox id='chk_3' name='chk_attribute_test'
checkbox id='chk_4' name='chk_attribute_test'

DIV ID="div_g5"

   文章导读:

  jQuery-Selectors(选择器)的行使(一、根基篇)

  jQuery-Selectors(选择器)的行使(二、条理篇)

  jQuery-Selectors(选择器)的行使(三、简朴篇)

  jQuery-Selectors(选择器)的行使(四-五、内容篇&可见性篇)

  jQuery-Selectors(选择器)的行使(六、属性篇)

  jQuery-Selectors(选择器)的行使(七、子元素篇)

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读