I want to hide all element with tag and contain a link

前端 未结 2 1379
慢半拍i
慢半拍i 2021-01-29 10:34
2条回答
  • 2021-01-29 10:37

    First of all, you are incorrectly using addEventListener. Second, you must check the href attribute, not innerHTML:

    window.addEventListener("yourEvent", () => {
      var elem=document.querySelectorAll("a");
      for(var i = 0; i < elem.length; i++) {
        if(elem[i].href.includes('sabdel.com')) {
          elem[i].style.display="none";
        }
      }
    }
    

    Replace yourEvent with the event you want to listen with. For example, if you are listening for a click event, replace it with click.

    0 讨论(0)
  • 2021-01-29 10:54

    Do you mean like this ?

    window.addEventListener = () => {
      var tags = document.getElementsByTagName("a");
    
      for (var i = 0; i < tags.length; i++) {
        var attributeValue = tags[i].getAttribute('href');
    
        if (attributeValue.includes('sabdel.com')) {
          tags[i].style.display = 'none';
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题