問題描述
根據文檔,有兩種方法可以獲取 ElementArrayFinder
中有多少元素(element.all()
調用的結果):
$$(".myclass").length
,記錄在 這里:
...數組的length
等于ElementArrayFinder
找到的元素的length
,每個結果代表執行的結果對元素的操作.
$$(".myclass").count()
,記錄在 這里:
計算ElementArrayFinder
所代表的元素個數.
這兩種方法有什么區別,應該首選哪一種?
$$(".myclass").length
需要解決 promise 以正確獲取元素的長度.
//工作$$(".myclass").then(function(items){項目長度;});//不工作$$(".myclass").length;
<小時>
$$(".myclass").count()
$$('.myclass').length
的包裝器,它本身就是一個 Promise,不需要像 .length
那樣解析 Promise
$$(".myclass").count();
<小時><塊引用>
應該首選哪一個?
除非在定位$$(".myclass")
和.then(function(items){...})
時涉及到一些復雜的業務,那么items.length
將提供更好的性能.
否則 $$(".myclass").count()
應始終使用.
According to the documentation, there are 2 ways to get how many elements are inside the ElementArrayFinder
(the result of element.all()
call):
$$(".myclass").length
, documented here:
...the array has
length
equal to thelength
of the elements found by theElementArrayFinder
and each result represents the result of performing the action on the element.
$$(".myclass").count()
, documented here:
Count the number of elements represented by the
ElementArrayFinder
.
What is the difference between these two methods and which one should be preferred?
$$(".myclass").length
Need to resolve the promise to get the length of element correctly.
// WORK
$$(".myclass").then(function(items){
items.length;
});
// DOES NOT WORK
$$(".myclass").length;
$$(".myclass").count()
A wrapper for $$('.myclass').length
which being a promise itself and doesn't require to resolve promise like .length
$$(".myclass").count();
which one should be preferred?
Unless there some complex business when locating $$(".myclass")
and .then(function(items){...})
involved then items.length
will give better performance.
Otherwise $$(".myclass").count()
should always be used.
這篇關于量角器中的計數()與長度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!