热得到.getElementsByTagName的属性?(Hot to get the attribute from .getElementsByTagName? [duplicate])

编程入门 行业动态 更新时间:2024-10-24 17:23:51
热得到.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 answers

Do 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> 
  
 

更多推荐

本文发布于:2023-08-06 14:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   getElementsByTagName   Hot   duplicate   attribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!