Selecting elements without jQuery

后端 未结 3 1973
南笙
南笙 2020-12-03 03:00

I guess this will be voted down, as it doesn\'t contain enough jQuery, but here it goes :)

What is the most effective way to get the element(s) returned by the jQuer

相关标签:
3条回答
  • 2020-12-03 03:26

    If you're using a modern browser, you could use this:

    window.top.document.querySelectorAll('a[title="some title text here"]')
    
    0 讨论(0)
  • 2020-12-03 03:26

    Not sure if it’s the most effective, but at least it works.

    var links = top.document.getElementsByTagName('a');
    var result = [];
    var linkcount = links.length;
    for ( var i = 0; i < linkcount; i++) {
        if (links[i].getAttribute('title') === 'some title text here') {
            result.push(links[i]);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:31

    Here is an example

    var getElements = function(tagName, attribute, value, callback) {
      var tags = window.document.getElementsByTagName(tagName);
      for (var i=0; i < tags.length; i++) {
        var tag = tags[i];
        if (tag.getAttribute(attribute) == value) {
          callback(tag);
        }
      };
    };
    
    getElements("a", "title", "PHP power player at Hettema & Bergsten. Click to learn more.", function(tag) {
      console.log(tag);
    });
    
    0 讨论(0)
提交回复
热议问题