简体   繁体   English

Javascript querySelectorAll on:可见元素不是有效的选择器

[英]Javascript querySelectorAll on :visible elements is not a valid selector

I'm trying to target some classes on my web page which are visible in the DOM, but for some reason I'm getting an error:我试图在我的 web 页面上定位一些在 DOM 中可见的类,但由于某种原因,我收到了一个错误:

Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document': '.form-control:visible' is not a valid selector.未捕获的 DOMException:无法在“文档”上执行“querySelectorAll”:“.form-control:visible”不是有效的选择器。

Surely this is valid, what am I missing?这当然有效的,我错过了什么?

My HTML:我的 HTML:

<input type="checkbox" class="form-control" />
<input type="checkbox" class="form-control" />
<input type="checkbox" class="form-control" />

My JS:我的 JS:

const inputs = document.querySelectorAll('.form-control:visible')

for (const [index, input] of inputs.entries()) {
    console.log(input)
}

What am I missing here?我在这里想念什么?

It is not valid, there is no :visible pseudo-class listed in the specification .这是无效的,规范中没有:visible伪类。

jQuery supports a :visible selector , but it is non-standard and you aren't using jQuery. jQuery 支持:visible选择器,但它是非标准的,您没有使用 jQuery。

The jQuery documentation says: jQuery 文档说:

Because:visible is a jQuery extension and not part of the CSS specification, queries using:visible cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method.因为:visible 是 jQuery 扩展而不是 CSS 规范的一部分,所以使用:visible 的查询不能利用本机 DOM querySelectorAll() 方法提供的性能提升。 To achieve the best performance when using:visible to select elements, first select the elements using a pure CSS selector, then use.filter(":visible").为了在使用时达到最佳性能:对 select 元素可见,首先 select 元素使用纯 CSS 选择器,然后使用.filter(":visible")。

Hy: There is no such ".visible" selector in JavaScript. Hy:JavaScript 中没有这样的“.visible”选择器。 ":visible" is a selector of jQuery which is used to create opacity 0 and 1 or shoe and hide. ":visible"是 jQuery 的选择器,用于创建不透明度 0 和 1 或鞋子和隐藏。 Thanks谢谢

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

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