开发者

JS: querySelectorAll with respect to z-index

开发者 https://www.devze.com 2023-01-18 21:05 出处:网络
I would like to select all \"div\" elements that are children of th开发者_如何转开发e \"body\" and have z-index: 2 property set.

I would like to select all "div" elements that are children of th开发者_如何转开发e "body" and have z-index: 2 property set.

document.querySelectorAll('body > div');

How do I involve the z-index check on this querySelector?


I believe that you can't involve the "z-index" into the selector, because there's no CSS selector syntax for selecting elements based on CSS style attributes.

What you could do would be to achieve your "z-index: 2" styling by giving elements a particular class. You could then use the class in your selector ("body > div.zIndex2" or whatever).


I would probably use jQuery for a selection like that and a custom selector like:

$.expr[':'].zIndex = function(obj) {
   if ($(obj).css('z-index')==2) return true;
   else return false;
}

Then call with:

$('body div:zIndex')

but you would need jQuery for this and I haven't tested it

0

精彩评论

暂无评论...
验证码 换一张
取 消