所以jQuery 1.6具有新功能prop().
$(selector).click(function(){
//instead of:
this.getAttribute('style');
//do i use:
$(this).prop('style');
//or:
$(this).attr('style');
})
Run Code Online (Sandbox Code Playgroud)
或者在这种情况下,他们做同样的事情?
如果我也有转用prop(),所有的旧attr()电话,如果我切换到1.6将打破?
UPDATE
selector = '#id'
$(selector).click(function() {
//instead of:
var getAtt = this.getAttribute('style');
//do i use:
var thisProp = $(this).prop('style');
//or:
var thisAttr = $(this).attr('style');
console.log(getAtt, thisProp, thisAttr);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id='id' style="color: red;background: orange;">test</div>Run Code Online (Sandbox Code Playgroud)
(另见这个小提琴:http://jsfiddle.net/maniator/JpUF2/)
控制台会记录getAttribute作为一个字符串,并attr作为一个字符串,但prop作为一个CSSStyleDeclaration,为什么?这对我未来的编码有何影响?
Serializable属性定义为:
getAttribute,您可以在DOM Inspector中看到更改.innerHTML元素的parentnode时,返回的html字符串将包含所有可序列化的属性作为它们的属性对应物我创建了一个页面,看起来它可以input在Chrome和Firefox中可靠地打印出该元素的所有可序列化属性的表格:http://jsfiddle.net/tEVLp/16/.自定义属性永远不可序列化,因此在firefox webkitSpeech等中不可序列化.在镀铬中测试以获得最佳效果.
所有布尔都是true因为false属性的序列化将缺少属性,这在测试中是假阴性.
所以我的问题是,为什么不是属性.value和.checked序列化?
从技术上讲,两者都是可序列化的..value只是一个字符串,浏览器没有序列化其他布尔属性的问题,例如.readOnly和.disabled.
我最好的猜测是,由于.defaultValue序列化为"value"-attribute和.defaultChecked序列化到"checked"-attribute,就不会有冲突,因此
.value并.checked不能序列.在这种情况下,为什么defaultX选择这些而不是那些反映更有用的电流.value和.checked状态的?