Javascript 查找所有 id 以公共字符串开头的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10111668/
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
Find all elements whose id begins with a common string
提问by meriley
I have a XSL that created multiple elements with the id of "createdOn" plus a $unique-id
我有一个 XSL,它创建了多个元素,ID 为“createdOn”加上一个 $unique-id
Example : createdOnid0xfff5db30
I want to find and store these in a variable using JavaScript. I've tried
我想使用 JavaScript 查找并将它们存储在变量中。我试过了
var dates = document.getElementsById(/createdOn/);
but that doesn't appear to work.
但这似乎不起作用。
回答by Phrogz
Using jQuery you can use the attr starts withselector:
var dates = $('[id^="createdOnid"]');
Using modern browsers, you can use the CSS3 attribute value begins withselector along with querySelectorAll
:
使用现代浏览器,您可以使用CSS3 属性值以选择器开头以及querySelectorAll
:
var dates = document.querySelectorAll('*[id^="createdOnID"]');
But for a fallback for old browsers (and without jQuery) you'll need:
但是对于旧浏览器(并且没有 jQuery)的回退,您需要:
var dateRE = /^createdOnid/;
var dates=[],els=document.getElementsByTagName('*');
for (var i=els.length;i--;) if (dateRE.test(els[i].id]) dates.push(els[i]);
回答by Isaac Fife
Because you didn't tag jQuery, and you probably don't need it, my suggestion would be to add a class to these elements when you create them. Then use the getElementsByClassName() function that's built into most browsers. For IE you would need to add something like this:
因为您没有标记 jQuery,而且您可能不需要它,所以我的建议是在创建这些元素时为这些元素添加一个类。然后使用大多数浏览器内置的 getElementsByClassName() 函数。对于IE浏览器,你将需要添加类似这样:
if (typeof document.getElementsByClassName!='function') {
document.getElementsByClassName = function() {
var elms = document.getElementsByTagName('*');
var ei = new Array();
for (i=0;i<elms.length;i++) {
if (elms[i].getAttribute('class')) {
ecl = elms[i].getAttribute('class').split(' ');
for (j=0;j<ecl.length;j++) {
if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
ei.push(elms[i]);
}
}
} else if (elms[i].className) {
ecl = elms[i].className.split(' ');
for (j=0;j<ecl.length;j++) {
if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
ei.push(elms[i]);
}
}
}
}
return ei;
}
}
回答by Daut
You should have just used simple CSS
selectortogether with JavaScript
's .querySelectorAll()
method.
您应该刚刚将简单CSS
选择器与JavaScript
's.querySelectorAll()
方法一起使用。
In your case :
在你的情况下:
var dates = document.querySelectorAll('[id^="createdOnId"]');
回答by kennebec
function idsLike(str){
var nodes= document.body.getElementsByTagName('*'),
L= nodes.length, A= [], temp;
while(L){
temp= nodes[--L].id || '';
if(temp.indexOf(str)== 0) A.push(temp);
}
return A;
}
idsLike('createdOn')
回答by PeterPink
Try the following:
请尝试以下操作:
var values = new Array(valueKey_1);
var keys = new Array("nameKey_1");
var form = document.forms[0];
for (var i = 0; i < form.length; i++) {
name = form.elements[i].name;
var startName = name.toLowerCase().substring(0, 18);
if (startName == 'startStringExample') {
values.push(name.value);
keys.push(name);
}
}