JSLite - 节点属性

2018-12-07 00:35 更新

如有疑问欢迎到这些地方交流,欢迎加入JSLite.io组织团伙共同开发!

segmentfault社区 | 官方网站 | 官方文档-更详细 | Issues

pluck

获取对象集合中每一个元素的属性值。

$("#box").pluck("nodeName") //⇒ ["DIV"]
$("#box").pluck("nextElementSibling") //⇒ <div class="boxs">1234567890</div>
$("#box").pluck("children") //⇒ [HTMLCollection[4]]

attr

读取或设置dom的属性。

$(".button").attr({"class":"","style":"background:red"}) //⇒ self 设置红色清空class
$(".button").attr("class","name")  //⇒ self 设置class替换之前的
$(".button").attr("class")  //⇒ string 获取class属性值

removeAttr

移动当前对象集合中所有元素的指定属性。

$("#box").removeAttr("class") //⇒ self 移除class

prop

读取或设置dom的属性。它在读取属性值的情况下优先于 attr,因为这些属性值会因为用户的交互发生改变,如 checkedselected

<input class="taiyang" id="check1" type="checkbox" checked="checked">
<input class="yaotaiyang" id="check2" type="checkbox">

$("#check1").attr("checked"); //=> "checked"
$("#check1").prop("checked"); //=> "true"
$("#check2").attr("checked"); //=> "false"
$("#check2").prop("checked"); //=> "false"
$("input[type="checkbox"]").prop("type",function(index,oldvalue){
console.log(index+"|"+oldvalue);
});
//=> 0|checkbox
//=> 1|checkbox
$("input[type="checkbox"]").prop("className",function(index,oldvalue){
console.log(index+"|"+oldvalue);
});
//=> 0|taiyang
//=> 1|yaotaiyang

removeProp

为集合中匹配的元素删除一个属性(property)。removeProp() 方法用来删除由.prop()方法设置的属性集。

注意: 不要使用此方法来删除原生的属性( property ),比如checked, disabled, 或者 selected。这将完全移除该属性,一旦移除,不能再次被添加到元素上。使用.prop()来设置这些属性设置为false代替。

<p id="n2" class="demo test" data-key="UUID" data_value="1235456465">CodePlayer</p>


var $n2 = $("#n2");
$n2.prop("prop_a", "CodePlayer");
$n2.prop("prop_b", { name: "CodePlayer", age: 20 } );


console.log($n2.prop("prop_a")) //⇒ CodePlayer
console.log($n2.prop("prop_b")) //⇒ Object {name: "CodePlayer", age: 20}


$n2.removeProp("data-key");
$n2.prop("data-key") //⇒ undefined
$n2.attr("data-key") //⇒ "UUID"

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号