开发者

Undefined href! (DOM API)

开发者 https://www.devze.com 2023-02-08 00:03 出处:网络
On google\'s search results page, document.getElementsByClassName(\"l\")[0] //\"l\" is the class for the URL\'s of the results.

On google's search results page,

document.getElementsByClassName("l")[0] 
//"l" is the class for the URL's of the results.

Returns undefined.

However,

alert(document.getElementsByClassName("l")[0]);

Will display the actual URL of the first result.

I just need to access the URL's of the results!

      //Adding
.href //to the end does not help

You 开发者_JS百科can experience this by going to your browser console and entering

document.getElementsByClassName("l")[0]


If you want just the href attribute:

document.getElementsByClassName('l')[0].getAttribute('href');

This works fine in Firefox/Firebug, Safari, and Chrome.


EDIT: it works fine in the search results, I was doing it in the homepage initially.

document.getElementsByClassName("l")
0

精彩评论

暂无评论...
验证码 换一张
取 消