根据属性值在DOM中查找元素

mic*_*ael 212 javascript dom

如果有任何DOM API搜索具有给定属性名称和属性值的元素,请告诉我:

就像是:

doc.findElementByAttribute("myAttribute", "aValue");
Run Code Online (Sandbox Code Playgroud)

Woj*_*ski 373

现代浏览器支持原生,querySelectorAll因此您可以:

document.querySelectorAll('[data-foo="value"]');
Run Code Online (Sandbox Code Playgroud)

https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll

浏览器兼容性详情:

您可以使用jQuery来支持过时的浏览器(IE9及更早版本):

$('[data-foo="value"]');
Run Code Online (Sandbox Code Playgroud)

  • 选择器应该是:`'[data-foo ="value"]'` (3认同)
  • 为了具体化“现代”定义:http://caniuse.com/#search=querySelectorAll (2认同)
  • 请注意,“jQuery”示例可能不是 jQuery。它可以是`$` 的Chrome 或Firefox 实现。在此处阅读更多相关信息:/sf/ask/1557137641/ (2认同)
  • 什么是“data-foo”......这个示例中的“myAttribute”去了哪里? (2认同)

Nic*_*ver 145

更新:在过去几年中,景观发生了巨大变化.您现在可以可靠地使用,querySelectorquerySelectorAll参见Wojtek的答案,了解如何执行此操作.

现在不需要jQuery依赖.如果您正在使用jQuery,那么很好......如果您不是,那么您不必再依赖于按属性选择元素了.


在vanilla javascript中执行此操作的方法不是很短,但有一些解决方案可用.

你做这样的事情,循环元素和检查属性

如果像jQuery这样的库是一个选项,你可以更轻松地做到这一点,如下所示:

$("[myAttribute=value]")
Run Code Online (Sandbox Code Playgroud)

如果该值不是有效的CSS标识符(其中包含空格或标点符号等),则需要围绕该值的引号(它们可以是单引号或双引号):

$("[myAttribute='my value']")
Run Code Online (Sandbox Code Playgroud)

你也可以做start-with,ends-with,contains等... 还有的属性选择多个选项.

  • 实际上,vanilla JavaScript DOM API在现代浏览器上运行良好 (4认同)
  • @WojtekKruszewski不是在2010年:)我确实更新了,希望提问者会为我们移动接受 - 我们想要当前的信息. (2认同)

小智 26

我们可以通过使用document.querySelector()document.querySelectorAll()方法在DOM中使用属性选择器.

为你的:

document.querySelector("selector[myAttribute='aValue']");
Run Code Online (Sandbox Code Playgroud)

并使用querySlectorAll():

document.querySelectorAll("selector[myAttribute='aValue']");
Run Code Online (Sandbox Code Playgroud)

querySelector()querySelectorAll()方法中,我们可以在"CSS"中选择对象.

有关"CSS"属性选择器的更多信息,请访问https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors


T.T*_*dua 19

FindByAttributeValue("Attribute-Name", "Attribute-Value");   
Run Code Online (Sandbox Code Playgroud)

ps如果您知道确切的元素类型,则添加第3个参数(即div, a, p ...etc...):

FindByAttributeValue("Attribute-Name", "Attribute-Value", "div");   
Run Code Online (Sandbox Code Playgroud)

但首先,定义此功能:

function FindByAttributeValue(attribute, value, element_type)    {
  element_type = element_type || "*";
  var All = document.getElementsByTagName(element_type);
  for (var i = 0; i < All.length; i++)       {
    if (All[i].getAttribute(attribute) == value) { return All[i]; }
  }
}
Run Code Online (Sandbox Code Playgroud)

每个评论建议更新ps.

  • 为什么?!你通过这样做循环你所有的DOM (6认同)
  • 这看起来很棒-如果页面上只有5个元素。 (2认同)
  • `document.querySelectorAll('[data-foo ="value"]');`由@Wojtek Kruszewski在接受的awnser上提出. (2认同)
  • 我们都知道这是3年前的事了 (2认同)

小智 13

您可以使用以下方法进行选择querySelector

document.querySelector("tagName[attributeName='attributeValue']")
Run Code Online (Sandbox Code Playgroud)


Dek*_*eke 9

你可以使用 getAttribute:

 var p = document.getElementById("p");
 var alignP = p.getAttribute("align");
Run Code Online (Sandbox Code Playgroud)

https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

  • 他想选择 `p` 元素而不使用 `id` 或 DOM 上的每个 `p`(和测试属性) (3认同)

GDe*_*nko 9

Daniel De Le\xc3\xb3n\'s 答案的修正
\n可以使用
\n ^=- 过滤器搜索 id(或任何其他 attr)以view关键字开头的元素

\n
document.querySelectorAll("[id^=\'view\']")\n
Run Code Online (Sandbox Code Playgroud)\n


VnD*_*vil 7

很简单,试试这个

<h1>The Document Object</h1>
<h2>The querySelector() Method</h2>

<h3>Add a background color to the first p element:</h3>
<p>This is a p element.</p>
<p data-vid="1">This is a p element.</p>
<p data-vid="2">This is a p element.</p>
<p data-vid="3">This is a p element.</p>

<script>
  document.querySelector("p[data-vid='1']").style.backgroundColor = "red";
  document.querySelector("p[data-vid='2']").style.backgroundColor = "pink";
  document.querySelector("p[data-vid='3']").style.backgroundColor = "blue";
</script>
Run Code Online (Sandbox Code Playgroud)


Dan*_*eón 6

使用查询选择器,示例:

document.querySelectorAll(' input[name], [id|=view], [class~=button] ')
Run Code Online (Sandbox Code Playgroud)

input[name]输入具有name属性的元素。

[id|=view]id 以 开头的元素view-

[class~=button]button类的元素。


Kha*_*Dev 5

这是一个示例,如何通过src属性搜索文档中的图像:

document.querySelectorAll("img[src='https://pbs.twimg.com/profile_images/........jpg']");
Run Code Online (Sandbox Code Playgroud)