从图像中提取源图像。

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

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

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

This is the js

  1. <script>
  2. var pic = document.getElementsByClassName("abc")[0].getElementsByTagName("img")[0];
  3. var src = pic.getAttribute('src');
  4. console.log(src);
  5. </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

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

This is the js

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

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

答案1

得分: 0

只需调用一次获取元素:

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

Just call once get element:

  1. 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:

确定