.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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章