开发者

Getting object of anchor tag using getElementById

开发者 https://www.devze.com 2023-03-29 21:53 出处:网络
anchorobject = document.getElementById(\'backbutton\'); alert(anchorobject); <a href=\"http://www.hotmail.com\" id=\"backbutton\">back</a>
anchorobject = document.getElementById('backbutton');
alert(anchorobject);

<a href="http://www.hotmail.com" id="backbutton">back</a>

The above code alerts the href attribute string (http://www.hotmail.com). Not the object itself. The file I am editing is just a local file which I want to use in some third party program in the future. First, I am coding it on my local computer. When I 开发者_Python百科try to get the object of a DIV it works just fine.

Why can't I get the object of an anchor (<a>) tag in JavaScript using document.getElementById()?


You are getting the anchor object. It's just that alert is a very poor debugging solution. If you alert an anchor object, it will just show you it's href. See this example. Instead, I would recommend using Firebug or Chrome with console.log.


How about this?

 alert(anchorobject.getAttribute("href"));


The anchorobject is your actuall anchor object, but if you use it as a string (in your case with an alert()), the object's toString() method creates a string from the href tag.

0

精彩评论

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