How to Get Element By Class in JavaScript?

前端 未结 11 2363
南旧
南旧 2020-11-22 01:48

I want to replace the contents within a html element so I\'m using the following function for that:

function ReplaceContentInContainer(id,content) {
   var c         


        
相关标签:
11条回答
  • 2020-11-22 02:26

    I think something like:

    function ReplaceContentInContainer(klass,content) {
    var elems = document.getElementsByTagName('*');
    for (i in elems){
        if(elems[i].getAttribute('class') == klass || elems[i].getAttribute('className') == klass){
            elems[i].innerHTML = content;
        }
    }
    }
    

    would work

    0 讨论(0)
  • 2020-11-22 02:26

    When some elements lack ID, I use jQuery like this:

    $(document).ready(function()
    {
        $('.myclass').attr('id', 'myid');
    });
    

    This might be a strange solution, but maybe someone find it useful.

    0 讨论(0)
  • 2020-11-22 02:36

    Of course, all modern browsers now support the following simpler way:

    var elements = document.getElementsByClassName('someClass');
    

    but be warned it doesn't work with IE8 or before. See http://caniuse.com/getelementsbyclassname

    Also, not all browsers will return a pure NodeList like they're supposed to.

    You're probably still better off using your favorite cross-browser library.

    0 讨论(0)
  • 2020-11-22 02:36

    I'm surprised there are no answers using Regular Expressions. This is pretty much Andrew's answer, using RegExp.test instead of String.indexOf, since it seems to perform better for multiple operations, according to jsPerf tests.
    It also seems to be supported on IE6.

    function replaceContentInContainer(matchClass, content) {
        var re = new RegExp("(?:^|\\s)" + matchClass + "(?!\\S)"),
            elems = document.getElementsByTagName('*'), i;
        for (i in elems) {
            if (re.test(elems[i].className)) {
                elems[i].innerHTML = content;
            }
        }
    }
    
    replaceContentInContainer("box", "This is the replacement text.");
    

    If you look for the same class(es) frequently, you can further improve it by storing the (precompiled) regular expressions elsewhere, and passing them directly to the function, instead of a string.

    function replaceContentInContainer(reClass, content) {
        var elems = document.getElementsByTagName('*'), i;
        for (i in elems) {
            if (reClass.test(elems[i].className)) {
                elems[i].innerHTML = content;
            }
        }
    }
    
    var reBox = /(?:^|\s)box(?!\S)/;
    replaceContentInContainer(reBox, "This is the replacement text.");
    
    0 讨论(0)
  • 2020-11-22 02:36

    This should work in pretty much any browser...

    function getByClass (className, parent) {
      parent || (parent=document);
      var descendants=parent.getElementsByTagName('*'), i=-1, e, result=[];
      while (e=descendants[++i]) {
        ((' '+(e['class']||e.className)+' ').indexOf(' '+className+' ') > -1) && result.push(e);
      }
      return result;
    }
    

    You should be able to use it like this:

    function replaceInClass (className, content) {
      var nodes = getByClass(className), i=-1, node;
      while (node=nodes[++i]) node.innerHTML = content;
    }
    
    0 讨论(0)
提交回复
热议问题