ChatGPT解决这个技术问题 Extra ChatGPT

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

您能否告诉我是否有任何 DOM API 可以搜索具有给定属性名称和属性值的元素:

就像是:

doc.findElementByAttribute("myAttribute", "aValue");
如果您正在寻找 Jquery Sol,请复制:stackoverflow.com/questions/696968/…
您能否将接受的答案更新为现代解决方案 Wojtek 的答案?

W
Wojtek Kruszewski

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

document.querySelectorAll('[data-foo="value"]');

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

有关浏览器兼容性的详细信息:

http://quirksmode.org/dom/core/#t14

http://caniuse.com/queryselector

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

$('[data-foo="value"]');

为了具体化“现代”定义:caniuse.com/#search=querySelectorAll
选择器应为:'[data-foo="value"]'
关于性能的任何注释?这比遍历所有节点更快吗?
请注意,“jQuery”示例可能不是 jQuery。它可以是 $ 的 Chrome 或 Firefox 实现。在此处阅读有关此内容的更多信息:stackoverflow.com/questions/22244823/…
什么是“data-foo”……在这个例子中,“myAttribute”去了哪里?
b
brasofilo

更新:在过去几年中,情况发生了巨大变化。您现在可以可靠地使用 querySelectorquerySelectorAll,请参阅 Wojtek's answer 了解如何执行此操作。

现在不需要 jQuery 依赖项。如果您使用的是 jQuery,那太好了……如果您不是,您不再需要仅仅依靠它来按属性选择元素。

在 vanilla javascript 中没有很短的方法可以做到这一点,但有一些可用的解决方案。

You do something like this, looping through elements and checking the attribute

如果像 jQuery 这样的库是一个选项,您可以更轻松地执行此操作,如下所示:

$("[myAttribute=value]")

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

$("[myAttribute='my value']")

您还可以执行 start-withends-withcontains 等...there are several options for the attribute selector


实际上 vanilla JavaScript DOM API 在现代浏览器上运行良好
@WojtekKruszewski 不是在 2010 年 :) 不过我确实更新了,希望提问者会为我们移动接受 - 我们想要当前信息。
我认为 jQuery(或等效的)可能是最简单的,尤其是跨浏览器兼容,而不必知道需要什么。
我不断得到一个空数组!我正在尝试通过它的 d 属性来获取 SVG,并且正在尝试 $("[d=path]");其中“路径”是一个包含我需要的特定 d 属性的变量。有没有人试过用 svg 路径来做这个?
a
albert

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

给你的:

document.querySelector("[myAttribute='aValue']");

并使用 querySelectorAll()

document.querySelectorAll("[myAttribute='aValue']");

querySelector()querySelectorAll() 方法中,我们可以像在“CSS”中选择一样选择对象。

https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors 中有关“CSS”属性选择器的更多信息


我不得不删除像这样的内引号 document.querySelectorAll("selector[myAttribute=aValue]");
T
T.Todua
FindByAttributeValue("Attribute-Name", "Attribute-Value");   

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

FindByAttributeValue("Attribute-Name", "Attribute-Value", "div");   

但首先,定义这个函数:

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]; }
  }
}

ps 根据评论建议更新。


为什么 ?!通过这样做,您正在循环所有 DOM
这看起来很棒 - 如果页面上只有 5 个元素。
document.querySelectorAll('[data-foo="value"]'); 由 @Wojtek Kruszewski 在接受的 awnser 上提出。
D
Daniel De León

使用查询选择器,示例:

document.querySelectorAll(' input[name], [id|=view], [class~=button] ')

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

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

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


D
Deke

你可以使用getAttribute:

 var p = document.getElementById("p");
 var alignP = p.getAttribute("align");

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


他想选择 p 元素而不使用 id 或 DOM 上的每个 p(和测试属性)
b
boubkhaled

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

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

S
Siddharth

使用 querySelector 进行选择的方法如下:

document.querySelector("tagName[attributeName='attributeValue']")

G
GDeriyenko

Amendment for Daniel De León's Answer
可以使用
^= 进行搜索 - 过滤 id(或任何其他 attr)以 view 关键字开头的元素

document.querySelectorAll("[id^='view']")

u
user17760986

函数 optCount(tagId, tagName, attr, attrval) { 输入 = document.getElementById(tagId).getElementsByTagName(tagName);如果(输入){ var reqInputs = [];输入计数 = 输入长度; for (i = 0; i < inputsCount; i++) { atts = inputs[i].attributes; var attsCount = atts.length; for (j = 0; j < attsCount; j++) { if (atts[j].nodeName == attr && atts[j].nodeValue == attrval) { reqInputs.push(atts[j].nodeName); } } } } else { alert("没有这样的指定标签存在"); } 返回 reqInputs.length; }//optcount函数关闭

这是一个用于选择具有特定属性值的特定标签的功能。要传递的参数是标签 ID,然后是标签名称 - 在该标签 ID 内,以及属性和第四个属性值。此函数将返回找到的具有指定属性的元素数量及其值。你可以根据自己的情况进行修改。


为什么在运行没有意义的情况下放置一个片段?