热得到.getElementsByTagName的属性?(Hot to get the attribute from .getElementsByTagName? [duplicate])
这个问题在这里已经有了答案:
querySelectorAll,getElementsByClassName和其他getElementsBy *方法返回什么? 8个答案不要为什么我不能通过使用函数getElementsByTagName来获取标签元素的属性值 。
此外,我也没有问题,直接使用acsess元素的id ,如来自<a id="t">.</a> t.getAttribute('href') <a id="t">.</a>
var a = document.body.getElementsByTagName('a').getAttribute('href'); console.log(a);<a href="###">http://internal.com/</a>This question already has an answer here:
What do querySelectorAll and getElementsBy* methods return? 9 answersDo not undertand why I can not get the attribute value of tag element by using function getElementsByTagName.
Also I have no problems with using direct acsess to the element by id , like t.getAttribute('href') from <a id="t">.</a>
var a = document.body.getElementsByTagName('a').getAttribute('href'); console.log(a);<a href="###">http://internal.com/</a>最满意答案
getElementsByTagName将返回一个数组,以便放入索引并获取所需的元素
var a = document.body.getElementsByTagName('a')[0].getAttribute('href'); console.log(a);<!DOCTYPE HTML> <html> <head> </head> <body> <a href="###">http://internal.com/</a> </body> </html>getElementsByTagName will return a array so put the index and get the element you want
var a = document.body.getElementsByTagName('a')[0].getAttribute('href'); console.log(a);<!DOCTYPE HTML> <html> <head> </head> <body> <a href="###">http://internal.com/</a> </body> </html>
更多推荐
发布评论