码迷,mamicode.com
首页 > 其他好文 > 详细

prop & attr

时间:2017-04-06 17:47:33      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:fine   class   ==   input   box   put   自定义   html   ted   

<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见

像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此需要使用prop方法去操作才能获得正确的结果。

$("#chk1").prop("checked") == false
$("#chk2").prop("checked") == true

如果上面使用attr方法,则会出现:

$("#chk1").attr("checked") == undefined
$("#chk2").attr("checked") == "checked"

对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。

prop & attr

标签:fine   class   ==   input   box   put   自定义   html   ted   

原文地址:http://www.cnblogs.com/happyhaibei/p/6674537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!