因此,jQuery 1.6引入了新函数prop()
。
$(selector).click(function(){
//instead of:
this.getAttribute('style');
//do i use:
$(this).prop('style');
//or:
$(this).attr('style');
})
或者在这种情况下它们做的是同样的事情吗?如果我必须改用prop(),那么如果我切换到1.6版本,所有旧的attr()调用都会出错吗?
更新:
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);
});
<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>
控制台将getAttribute
输出为字符串,将attr
输出为字符串,但将prop
输出为CSSStyleDeclaration
类型。为什么?这对我的未来编程有什么影响吗?
(另请参考此fiddle:http://jsfiddle.net/maniator/JpUF2/)