My Greasemonkey script is not finding the elements (links) on the page?

前端 未结 2 878
独厮守ぢ
独厮守ぢ 2020-12-12 04:55

The website is: lexin.nada.kth.se/lexin/#searchinfo=both,swe_gre,hej;

My script is:

function main(){
  var links=document.getElementsByTagName(\"a\")         


        
2条回答
  •  臣服心动
    2020-12-12 05:19

    It's returning an HTMLcollection because of .getElementsByTagName and because of that, you will have to state the HTMLcollection with .getElementsByTagName and then find the length, and alert it. It will look like this...

     (function main(){
            var links = document.getElementsByTagName("a").length
            alert("There are "+ links + " links.");
            })()

    I added an IIFE or an Immediately-Invoked-Function-Expression more on IIFEs here, so you don't have to call the function, and so the code is small and able to be "swallowed". lastly, it's alerting 2 alert boxes, because there's one[alert box] in the function and you're calling that function so it's going to do the same thing.

提交回复
热议问题