美文网首页
jQuery中attr和prop的区别

jQuery中attr和prop的区别

作者: 想成为咸鱼的张三 | 来源:发表于2018-06-15 15:29 被阅读0次

https://blog.csdn.net/xiaouncle/article/details/53959496 文档 仅供复习用
对于HTML元素本身就带有的固有属性,在处理时建议使用prop方法
对于HTML元素我们自定义的DOM属性,在处理时建议使用attr方法

<a href="#" target="_self">超链接</a>
<input type="checkbox" id="testCheckBox" value="测试CheckBox"/>
alert($("a").attr("id"));//输出:undefine
alert($("a").prop("id"));//输出:默认值""
alert($("a").attr("checked"));//输出:undefine
alert($("a").prop("checked"));//输出:undefine
alert($("#testCheckBox").attr("checked"));//输出:undefine
alert($("#testCheckBox").prop("checked"));//输出:默认值false
alert($("a").attr("class"));//输出:undefine
alert($("a").prop("class"));//输出:默认值""

否则会造成类似 多次使用input的checked属性不显示问题

相关文章

网友评论

      本文标题:jQuery中attr和prop的区别

      本文链接:https://www.haomeiwen.com/subject/nirmeftx.html