JQUERY prop与attr差额

时间:2021-01-27 21:54:33

1.  1-9-1之前和之后之间的差

<html>
<script src="Js/jquery-1.9.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$(":button").click(function(){
//prop与attr差别
//attr在1.9以后attr没有初始化的.attr("checked")返回都是undefined
console.log("prop1="+$(":checkbox:eq(0)").prop("checked"));
console.log("attr1="+$(":checkbox:eq(0)").attr("checked"));
//attr在1.9以后attr初始化的不管是否选中.attr("checked")返回都是checked
console.log("prop2="+$(":checkbox:eq(1)").prop("checked"));
console.log("attr2="+$(":checkbox:eq(1)").attr("checked"));
}) })
</script>
<body>
<input type="button" value="bt" id="bt"/>
<input type="checkbox" id="ck" value="1" /><label>1</label>
<input type="checkbox" id="ck" value="2" checked="checked"/><label>2</label>
</body>
</html>

2.  使用方法

//3种
.attr("name");
.attr("name","value");
.attr({"name":"value"})
//4种
.prop("name");
.prop("name","value");
.prop({"name":"value"})
.prop("checked",function(){
return true;
})

版权声明:本文博客原创文章,博客,未经同意,不得转载。