查找所有id以公共字符串开头的元素

时间:2022-05-31 19:27:08

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.

但这似乎行不通。

4 个解决方案

#1


51  

Using jQuery you can use the attr starts with selector:

使用jQuery您可以使用attr从选择器开始:

var dates = $('*[id^="createdOnid"]');

Using modern browsers, you can use the CSS3 attribute value begins with selector 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]);

#2


6  

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

#3


2  

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')

#4


1  

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

#1


51  

Using jQuery you can use the attr starts with selector:

使用jQuery您可以使用attr从选择器开始:

var dates = $('*[id^="createdOnid"]');

Using modern browsers, you can use the CSS3 attribute value begins with selector 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]);

#2


6  

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

#3


2  

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')

#4


1  

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