正在回答 回答被采纳积分+1
4回答
樱桃小胖子
2018-04-03 16:57:46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title ></ title > < style type = "text/css" > #check{ width: 50px; height: 50px; } </ style > </ head > < body > < input type = "checkbox" id = "check" tabindex = "1" title = "Check this!" description = "just a checkbox" checked = 'ture' /> < script src = "js/jquery-2.1.0.js" ></ script > < script > /* 元素的特性和属性值的获取 attributes(特性):值为string properties(属性):值为string、boolean、number、object 元素特性和属性值的区别 1、如果特性attributes 是本来在DOM对象中就存在的,attributes和properties(属性)的值会同步。 2、如果attributes是本来在DOM对象中就存在的,但是类型为Boolean,那么attributes和properties的值不会同步; 3、如果attributes不是DOM对象内建的属性,attributes和properties的值不会同步; */ $(function(){ var checkbox =document.getElementById('check'); //如果特性attributes 是本来在DOM对象中就存在的,attributes和properties(属性)的值会同步。 //元素的属性和特性会保持动态链接,会同时改变 //设置属性 checkbox.title ="new title"; //设置特性 checkbox.setAttribute('title','another title'); console.log(jQuery.type(checkbox.getAttribute('title')));//String console.log(jQuery.type(checkbox.title));//String console.log(checkbox.getAttribute('title') === checkbox.title);//特性与属性同时改变且相等 //如果attributes是本来在DOM对象中就存在的,但是类型为Boolean,那么attributes和properties的值不会同步; checkbox.checked =false; checkbox.setAttribute('checked',false); console.log(jQuery.type(checkbox.checked));//Boolean console.log(jQuery.type(checkbox.getAttribute('checked')));//String }) </ script > </ body > </ html > |
这两个并不冲突,而是两种状态,建议参考代码理解一下~
相似问题
登录后可查看更多问答,登录/注册
前端小白入门系列课程
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36712 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧