javascript 正则表达式 getElementById
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12566888/
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
Regular expression getElementById
提问by Emphram Stavanger
I need to use pure Javascript for the first time in a long while, and having gotten used to the comfy mattress of jQuery, all the important stuff is escaping me.
我需要在很长一段时间内第一次使用纯 Javascript,并且已经习惯了 jQuery 的舒适床垫,所有重要的东西都在逃避我。
I need to select a bunch of divs on regular expression. So I have stuff like this;
我需要在正则表达式上选择一堆 div。所以我有这样的东西;
<div id="id_123456_7890123"> .. </div>
<div id="id_123456_1120092"> .. </div>
<div id="id_555222_1200192"> .. </div>
<div id="id_123456_9882311"> .. </div>
And I'd need to create a loop that goes through all the divs with an id that begins with id_123456_
. How would I go about doing that?
我需要创建一个循环,遍历所有以 .id 开头的 div id_123456_
。我该怎么做呢?
I used jQuery with the :regex
filter plugin before, but looking at it, it doesn't seem like there's much I could salvage in a pure javascript rewrite.
我之前将 jQuery 与:regex
过滤器插件一起使用,但看着它,似乎我无法在纯 javascript 重写中挽救多少。
回答by jfriend00
In plain javascript, you could do this generic search which should work in every browser:
在普通的 javascript 中,您可以执行此通用搜索,它应该适用于每个浏览器:
var divs = document.getElementsByTagName("div"), item;
for (var i = 0, len = divs.length; i < len; i++) {
item = divs[i];
if (item.id && item.id.indexOf("id_123456_") == 0) {
// item.id starts with id_123456_
}
}
Working example: http://jsfiddle.net/jfriend00/pYSCq/
回答by Alnitak
This works by recursively traversing the whole DOM.
这是通过递归遍历整个 DOM 来实现的。
It's possibly not the most efficient, but should work on every browser.
它可能不是最有效的,但应该适用于所有浏览器。
function find_by_id(el, re, s) {
s = s || [];
if (el.tagName === 'DIV' && re.exec(el.id) !== null) {
s.push(el);
}
var c = el.firstChild;
while (c) {
find_by_id(c, re, s);
c = c.nextSibling;
}
return s;
}
var d = find_by_id(document.body, /^id_123456_/);
回答by howderek
Here you are: http://jsfiddle.net/howderek/L4z9Z/
你在这里:http: //jsfiddle.net/howderek/L4z9Z/
HTML:
HTML:
<div id="nums">
<div id="id_123456_7890123">Hey</div>
<div id="id_123456_1120092">Hello</div>
<div id="id_555222_1200192">Sup</div>
<div id="id_123456_9882311">Boom</div>
</div>
<br/>
<br/>
<div id="result"></div>?
Javascript:
Javascript:
divs = document.getElementsByTagName("div");
divsWith123456 = new Array();
for (var i = 0;i < divs.length;i++) {
if (divs[i].id.match("id_123456") != null) {
divsWith123456.push(divs[i]);
document.getElementById("result").innerHTML += "Found: divs[" + i + "] id contains id_123456, its content is \"" + divs[i].innerHTML + "\"<br/><br/>";
}
}?
回答by paje007
HTML DOM querySelectorAll() method will work here.
HTML DOM querySelectorAll() 方法将在这里工作。
document.querySelectorAll('[id^="id_"]');
Borrowed from StackOverFlow here