简体   繁体   English

属性名称模式的JavaScript / CSS选择器

[英]JavaScript/CSS selector for attribute name pattern

Assuming I have the following markup 假设我有以下标记

<div data-common-notcommon="anyValue">
    <!-- Whatever.. -->
</div>

<div data-common-not-commonerthing="anyValue">
    <!-- Whatever.. -->
</div>

I'm trying to write a JS selector (or a CSS selector, don't care for the difference..) to find attributes based on a common, partial attribute name. 我正在尝试编写JS选择器(或CSS选择器,不在乎它们之间的区别..),以基于通用的部分属性名称查找属性。

IE I want to find all the elements that start with data-common . IE浏览器我想找到所有以data-common开头的元素。

I can't find anything on Google for attribute name selectors but rather attribute value selectors which I don't really want to do. 我在Google上找不到用于属性名称选择器的任何内容,而是我真正不想做的属性值选择器。

<script>
  document.querySelectorAll('[data-common]'); // []
  document.querySelectorAll('[data-common*]'); // []
  // etc..
</script>

Currently there are no selectors defined to partially match attribute names. 当前,没有定义用于部分匹配属性名称的选择器。 What you're asking for doesn't exist. 您所要求的不存在。

For JavaScript you could filter a collection of elements using custom code (that is what jQuery does), but it will not work with document.querySelectorAll , nor can you define a custom selector for CSS, unless you're willing to suggest it on the w3c mailing list and deal with navigating the complex workflow that's involved in changing the CSS language. 对于JavaScript,您可以使用自定义代码(这就是jQuery的功能)过滤元素集合,但是它不适用于document.querySelectorAll ,也不能为CSS定义自定义选择器,除非您愿意在w3c邮件列表,并处理导航更改CSS语言所涉及的复杂工作流程。

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

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