Find all elements whose id begins with a common string

前端 未结 5 701
臣服心动
臣服心动 2020-12-01 09:01

I have a XSL that created multiple elements with the id of \"createdOn\" plus a $unique-id

Example : createdOnid0xfff5db30

I want to find a

相关标签:
5条回答
  • 2020-12-01 09:24

    You should have just used simple CSS selector together with JavaScript's .querySelectorAll() method.

    In your case :

    var dates = document.querySelectorAll('[id^="createdOnId"]');
    
    0 讨论(0)
  • 2020-12-01 09:25

    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:

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 09:25
    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')
    
    0 讨论(0)
  • 2020-12-01 09:44

    Using jQuery you can use the attr starts with selector:

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

    Using modern browsers, you can use the CSS3 attribute value begins with selector along with querySelectorAll:

    var dates = document.querySelectorAll('*[id^="createdOnID"]');
    

    But for a fallback for old browsers (and without jQuery) you'll need:

    var dateRE = /^createdOnid/;
    var dates=[],els=document.getElementsByTagName('*');
    for (var i=els.length;i--;) if (dateRE.test(els[i].id]) dates.push(els[i]);
    
    0 讨论(0)
  • 2020-12-01 09:45

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题