Get HTML element via aria label

后端 未结 3 972
不思量自难忘°
不思量自难忘° 2021-02-07 02:48

I\'m making a small chrome extension and for it I need to grab a div from the DOM to manipulate. I get the DOM but I\'m having trouble grabbing the required d

3条回答
  •  情歌与酒
    2021-02-07 03:05

    this is the permenent solution check it it will work forever

    var getElementsByAttribute = function (attr, value) {
    var match = [];
    
    var elements = document.getElementsByTagName("*");
    
    for (var ii = 0, ln = elements.length; ii < ln; ii++) {
    
    if (elements[ii].hasAttribute(attr)) {
    
      /* If a value was passed, make sure it matches the element's */
      if (value) {
    
        if (elements[ii].getAttribute(attr) === value) {
          match.push(elements[ii]);
        }
        } else {
    
        match.push(elements[ii]);
      }
    }
     }
      return match;
    };
    
    (function () {
      var baz = getElementsByAttribute('data-foo', 'bar');
      for (var xx = 0, ln = baz.length; xx < ln; xx++) {
        baz[xx].innerHTML = 'These *are* the droids we are looking for!';
     }
    })();
    

提交回复
热议问题