.prop() 與 .attr() - .prop() vs .attr()

問題:

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爲字符串,但將propCSSStyleDeclaration ,爲什麼? And how does that affect my coding in the future?這對我將來的編碼有何影響?


解決方案:

參考一: https://en.stackoom.com/question/OeGS
參考二: https://stackoom.com/question/OeGS
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章