简体   繁体   English

将jquery选择器转换为数组

[英]Convert jquery selectors to array

I need to be able to store the current selectors in the current viewport and then 10 seconds check if they are still in the users current viewport. 我需要能够将当前选择器存储在当前视口中,然后10秒检查它们是否仍在用户当前视口中。

My solution for this was to store the selectors in an array and then in 10 seconds compare the old selectors against the new and see if any match. 我的解决方案是将选择器存储在一个数组中,然后在10秒内将旧选择器与新选择器进行比较,看看是否有任何匹配。 If they do... do something. 如果他们这样做......做某事。

So i believe using .each and build the array, unless somebody has a more elegant solution to this? 所以我相信使用.each并构建数组,除非有人对此有更优雅的解决方案?

$('.gridContainers:in-viewport')

This will return a standard selectors. 这将返回标准选择器。

Calling $(selector) returns an array-like jQuery object, not an actual JavaScript array, though for the purposes of what they're trying to do converting it to an actual array may be unnecessary. 调用$(selector)返回一个类似于数组的jQuery对象,而不是一个真正的JavaScript数组,但是为了他们试图将它转换为实际数组的目的可能是不必要的。

This is how one would turn a selector into an native Javascript array. 这就是将选择器转换为本机Javascript数组的方式。

$(selector).toArray()

Jquery.toArray() Jquery.toArray()

使用ES6

Array.from($(selector)); // return JS array 

Try Out FIND method as Below: 试试FIND方法如下:

$('element').find('selection');

This will give all selected elements to Array. 这将为Array提供所有选定的元素。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM