jQuery length 和 size()區(qū)別總結(jié)
jQuery length和size()區(qū)別總結(jié)如下:
1.length是屬性,size()是方法。
2.如果你只是想獲取元素的個數(shù),兩者效果一樣既 ("img").length
和("img").length
和("img").size()
獲取的值是一樣的。
3.如果計算一個字符串的長度或者計算一個數(shù)組元素的個數(shù)就只得用length, 如 $("#text").val().length
。
看看他們的執(zhí)行時間,http://jsperf.com/size-vs-length用這個檢測的
從圖中可以看到size()方法比length慢38%,原因何在?
原因在此:
看看官網(wǎng)的解釋(http://api.jquery.com/size/):
The .size() method is deprecated as of jQuery 1.8. Use the .length property instead.
The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.
從上可以看出size()是調(diào)用length屬性實現(xiàn)的
在jquery 1.8后 length取代了 size() ,因為length不需要返回一個函數(shù)調(diào)用,更優(yōu)秀。
Learn from yesterday, live for today, hope for tomorrow.
總結(jié)
以上所述是小編給大家介紹的jQuery length 和 size()區(qū)別總結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jQuery實現(xiàn)簡單的網(wǎng)頁換膚效果示例
這篇文章主要介紹了jQuery實現(xiàn)簡單的網(wǎng)頁換膚效果,涉及jQuery事件響應(yīng)及頁面元素屬性動態(tài)變換操作技巧,需要的朋友可以參考下2016-09-09jQuery中$.get、$.post、$.getJSON和$.ajax的用法詳解
本文重點是來講講jQuery中調(diào)用ajax的4種方法:$.get、$.post、$getJSON、$ajax。如果讀者沒有javascript和jquery的知識,或者沒有ajax的概念,那么請先去問問度娘,再來讀本文。2014-11-11