使用getElementById获取定位标记的对象 [英] Getting object of anchor tag using getElementById
问题描述
anchorobject = document.getElementById('backbutton');
alert(anchorobject);
<a href="http://www.hotmail.com" id="backbutton">back</a>
上面的代码警告href属性字符串(http://www.hotmail.com
).不是对象本身.我正在编辑的文件只是一个本地文件,将来我想在某些第三方程序中使用.首先,我正在本地计算机上对其进行编码.当我尝试获取DIV
的对象时,效果很好.
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 try to get the object of a DIV
it works just fine.
为什么我不能使用document.getElementById()
在JavaScript中获得锚点(<a>
)标签的对象?
Why can't I get the object of an anchor (<a>
) tag in JavaScript using document.getElementById()
?
推荐答案
您正在获取锚对象.只是alert
是一个非常差的调试解决方案.如果警报锚对象,它将仅显示它为href
.请参见此示例.相反,我建议将Firebug或Chrome与console.log
一起使用.
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
.
这篇关于使用getElementById获取定位标记的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!