Home  >  Article  >  Web Front-end  >  How to use jQuery length and size()

How to use jQuery length and size()

php中世界最好的语言
php中世界最好的语言Original
2018-06-02 10:07:551561browse

This time I will show you how to use jQuery length and size(), and what are the precautions for using jQuery length and size(). The following is a practical case, let's take a look.

1.length is the

attribute, and size() is the method.

2. If you just want to get the number of elements, the two effects are the same,

("img").length and ("img").length The value obtained by ("img").size() is the same.

3. If you calculate the length of a

string or the number of elements in an array, you have to use length, such as $("#text").val(). length.

Take a look at their execution time, http://jsperf.com/size-vs-length uses this detection

You can see from the picture The size() method is 38% slower than length. Why?

The reason is here:

Look at the explanation on the official website (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.

As can be seen from the above, size() is implemented by calling the length property

After jquery 1.8, length replaced size() , because length does not need to return a function call, which is better.

Learn from yesterday, live for today, hope for tomorrow.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!

Recommended reading:

How to use Vue to achieve drag-and-drop effect

Node.js console output log file example analysis

The above is the detailed content of How to use jQuery length and size(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn