Firefox, query selector and the visible pseudo selector

无人久伴 提交于 2019-12-10 02:13:41

问题


Is there anyway to use a pseudo selector with Firefox's querySelector() or querySelectorAll() functions to detect visibility? In particular I want to be able to do something like this:

elem.querySelector('#list .list-item:visible');
elem.querySelector('#section .sub-section:visible .title');

No need to worry about browser inconsistencies or other implementation, just Firefox. Thanks!

EDIT: Visible is defined by display not being none and visibility not being hidden.


回答1:


No, there isn't. The CSS specification doesn't define a :visible (or related) selector, and AFAIK Firefox doesn't implement non-standard pseudo selectors.

If you'd like to implement this yourself, note how jQuery implements its :visible selector:

In jQuery 1.3.1 (and older) an element was visible if its CSS "display" was not "none", its CSS "visibility" was not "hidden", and its type (if it was an input) was not "hidden". In jQuery 1.3.2 an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0.

Source: http://docs.jquery.com/Release:jQuery_1.3.2#:visible.2F:hidden_Overhauled




回答2:


Since there is no native implimentation of the :visible pseudo selector I decided to use CSS classes to hide and show my elements, thus allowing to simply just check for the class name instead of visibility. Here is what my selector looks like now:

elem.querySelector('#list .list-item:not(.hidden)');

Now in 2016 we can also use the hidden html5 attribute, so this selector would work too:

elem.querySelector('#list .list-item:not([hidden])');



回答3:


Using plain javascript, you could also emulate jQuery behaviour easily, turning your querySelector results into an array, and filtering it:

Array.prototype.slice.call(document.querySelectorAll('your selector'))

That creates a plain array with the results of your selector, that you can filter as:

.filter(function (item,index) { return item.style.display!="none" } );

or even the rest of jquery conditions (item.style.visibility != "hidden" && item.style.opacity > 0 && ...).

As a one liner:

Array.prototype.slice.call(document.querySelectorAll('your selector')).filter(function (item,index) { return item.style.display!="none" } );



回答4:


For finding elements that are not display:none the CSS selector equivalent to :visible is

:not([style='display:none'])

You could do the same for visibility:hidden and then chain :not() selectors if you need to.

Here's a fiddle.

Edit: Be careful with spaces and other punctuation. If you later manipulate these elements with JQuery and hide(), for instance, and need to call the same function, then you will need to chain :not([style="display: none;"]) to your CSS selector.




回答5:


Checking if element is visible, supports on all major browsers:

jQuery:

$('.list-item').is(':visible');

Vanilla JS:

function isVisible(elem) {return elem.offsetWidth > 0 || elem.offsetHeight > 0 || elem.getClientRects().length > 0; }


来源:https://stackoverflow.com/questions/13388616/firefox-query-selector-and-the-visible-pseudo-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!