問題:
So jQuery 1.6 has the new function prop()
.所以jQuery 1.6有了新的函數prop()
。
$(selector).click(function(){
//instead of:
this.getAttribute('style');
//do i use:
$(this).prop('style');
//or:
$(this).attr('style');
})
or in this case do they do the same thing?或者在這種情況下,他們做同樣的事情嗎?
And if I do have to switch to using prop()
, all the old attr()
calls will break if i switch to 1.6?如果我必須切換到使用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); });
<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>
(see also this fiddle: http://jsfiddle.net/maniator/JpUF2/ ) (另見這個小提琴: http : //jsfiddle.net/maniator/JpUF2/ )
The console logs the getAttribute
as a string, and the attr
as a string, but the prop
as a CSSStyleDeclaration
, Why?控制檯將getAttribute
記錄爲字符串,將attr
爲字符串,但將prop
爲CSSStyleDeclaration
,爲什麼? And how does that affect my coding in the future?這對我將來的編碼有何影響?
解決方案:
參考一: https://en.stackoom.com/question/OeGS參考二: https://stackoom.com/question/OeGS