因此jQuery 1.6具有新功能prop()
。
$(selector).click(function(){
//instead of:
this.getAttribute('style');
//do i use:
$(this).prop('style');
//or:
$(this).attr('style');
})
还是在这种情况下他们做同样的事情?
如果我确实必须切换到using prop()
,attr()
那么如果我切换到1.6 ,所有旧呼叫都会中断?
更新
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>
(另请参见此提琴:http : //jsfiddle.net/maniator/JpUF2/)
控制台将记录getAttribute
为字符串,将记录attr
为字符串,但是将记录prop
为CSSStyleDeclaration
,为什么?将来如何影响我的编码?
Gary Hole answer is very relevant to solve the problem if the code is written in such way
Since the prop function return
CSSStyleDeclaration
object, above code will not working properly in some browser(tested withIE8 with Chrome Frame Plugin
in my case).Thus changing it into following code
solved the problem.