从图像中提取源图像。

huangapple go评论79阅读模式
英文:

Retrieve the src from a image

问题

I'm trying to get the src in a specific image on my webpage. When I entered my code the output is an undefined error.

Uncaught TypeError: Cannot read properties of undefined (reading 'getAttribute')

This is the HTML code

<div class="team-member-image border-radius-5px overflow-hidden">
      <img class="abc" alt="" src="images/team01.jpg">
</div>

This is the js

<script>
      var pic = document.getElementsByClassName("abc")[0].getElementsByTagName("img")[0];
      var src = pic.getAttribute('src');
      console.log(src);
</script>

What can be the issue in this and how can I solve this.

英文:

I'm trying to get the src in a specific image on my webpage. When I entered my code the output is an undefined error.

Uncaught TypeError: Cannot read properties of undefined (reading 'getAttribute')

This is the HTML code

&lt;div class=&quot;team-member-image border-radius-5px overflow-hidden&quot;&gt;
      &lt;img class=&quot;abc&quot; alt=&quot;&quot; src=&quot;images/team01.jpg&quot;&gt;
&lt;/div&gt;

This is the js

`&lt;script&gt;
      var pic = document.getElementsByClassName(&quot;abc&quot;)[0].getElementsByTagName(&quot;img&quot;)[0];
      var src = pic.getAttribute(&#39;src&#39;);
      console.log(src);
&lt;/script&gt;`

What can be the issue in this and how can I solve this.

答案1

得分: 0

只需调用一次获取元素:

var pic = document.getElementsByClassName("abc")[0];
英文:

Just call once get element:

var pic = document.getElementsByClassName(&quot;abc&quot;)[0];

huangapple
  • 本文由 发表于 2023年7月11日 12:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定