Javascript getElementById() 通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4275071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getElementById() wildcard
提问by Florian Müller
I got a div, and there i got some childnodes in undefined levels.
我有一个 div,在那里我有一些未定义级别的子节点。
Now I have to change the ID of every element into one div. How to realize?
现在我必须将每个元素的 ID 更改为一个 div。如何实现?
I thought, because they have upgoing IDs, so if the parent is id='path_test_maindiv' then the next downer would be 'path_test_maindiv_child', and therefore I thought, I'd solve that by wildcards, for example:
我想,因为他们有上行 ID,所以如果父母是 id='path_test_maindiv' 那么下一个 downer 将是'path_test_maindiv_child',因此我想,我会通过通配符来解决这个问题,例如:
document.getElementById('path_test_*')
Is this possible? Or are there any other ways?
这可能吗?或者还有其他方法吗?
回答by joksnet
Not in native JavaScript. You have various options:
不是在原生 JavaScript 中。您有多种选择:
1) Put a class and use getElementsByClassName but it doesn't work in every browser.
1)放置一个类并使用 getElementsByClassName 但它不适用于每个浏览器。
2) Make your own function. Something like:
2)制作自己的函数。就像是:
function getElementsStartsWithId( id ) {
var children = document.body.getElementsByTagName('*');
var elements = [], child;
for (var i = 0, length = children.length; i < length; i++) {
child = children[i];
if (child.id.substr(0, id.length) == id)
elements.push(child);
}
return elements;
}
3) Use a library or a CSS selector. Like jQuery ;)
3) 使用库或 CSS 选择器。像 jQuery ;)
回答by Felix Kling
In one of the comments you say:
在您的评论之一中,您说:
(...) IE is anyway banned on my page, because he doesn't get it with CSS. It's an admin tool for developer, so only a few people, and they will anyway use FF
(...) 无论如何,IE 在我的页面上被禁止,因为他没有使用 CSS。这是开发人员的管理工具,所以只有少数人,他们无论如何都会使用FF
I think you should follow a different approach from the beginning, but for what it's worth, in the newer browsers (ok, FF3.5), you can use document.querySelectorAll()
with which you can get similar results like jQuery:
我认为您应该从一开始就采用不同的方法,但就其价值而言,在较新的浏览器(好吧,FF3.5)中,您可以使用document.querySelectorAll()
它可以获得类似 jQuery 的结果:
var elements = document.querySelectorAll('[id^=foo]');
// selects elements which IDs start with foo
Update:querySelectorAll()
is only notsupported in IE < 8 and FF 3.0.
更新:querySelectorAll()
只未在IE <8和FF 3.0支持。
回答by Jamiec
jQuery allows you to find elements where a particular attribute starts with a specific value
jQuery 允许您查找特定属性以特定值开头的元素
In jQuery you would use
在 jQuery 中,你会使用
$('[id^="path_test_"]')
回答by Ivan Kolyhalov
Try this in 2019 as a wildcard.
在 2019 年试试这个作为通配符。
document.querySelectorAll("[id*=path_test_]")
回答by Asif Mulla
I don't think so wildcards are allowed in getelementById. Instead you can have all the child nodes of your respective DIV using:
我认为 getelementById 中不允许使用通配符。相反,您可以使用以下方法拥有各自 DIV 的所有子节点:
var childNodeArray = document.getElementById('DIVID').childNodes;
This'll give you an array of all elements inside your DIV. Now using for
loop you can traverse through all the child elements and simultaneously you can check the type of element or ID or NAME, if matched then change it as you want.
这将为您提供 DIV 中所有元素的数组。现在使用for
循环,您可以遍历所有子元素,同时您可以检查元素或 ID 或 NAME 的类型,如果匹配,则根据需要进行更改。
回答by Shadow Wizard is Ear For You
You should not change ID of element to ID of other existing element. It's very wrong, so you better re-think your core logic before going on.
您不应将元素的 ID 更改为其他现有元素的 ID。这是非常错误的,所以你最好在继续之前重新思考你的核心逻辑。
What are you trying to do exactly?
你到底想做什么?
Anyway, to getall elements with ID starting with something, use jQuery as suggested before, if you can't it's also possible using pure JavaScript, let us know.
无论如何,要获取所有带有 ID 的元素,请按照之前的建议使用 jQuery,如果不能,也可以使用纯 JavaScript,请告诉我们。