【问题标题】:Retrieve value from img tag [duplicate]从img标签中检索值[重复]
【发布时间】:2021-11-14 05:18:21
【问题描述】:

我想获取favoriteid 的值。

<td class="favoriteRow" style="">

  <img src="/images/starGold.png" loading="lazy" favoriteid="bitcoin">

</td>

这是我尝试过的,但得到了回复undefined。

console.log(this.childNodes[0]['favoriteid'])

console.log(this) 输出:

<td class="favoriteRow" style="">

  <img src="/images/starGold.png" loading="lazy" favoriteid="bitcoin">

</td>

【问题讨论】:

  • 很奇怪,你想记录 this 和 this.childNodes[0].favoriteid 但从未记录 this.childNodes 或 this.childNodes[0] 以查看它们是什么。

标签: javascript html


【解决方案1】:

这样的?

见docs!

const img = document.querySelector("img[favoriteid]");
console.log( img.getAttribute("favoriteid") )
<td class="favoriteRow" style="">
  <img src="/images/starGold.png" loading="lazy" favoriteid="bitcoin">
</td>

或

document.querySelector(".favoriteRow img").getAttribute("favoriteid")

console.log(document.querySelector(".favoriteRow img").getAttribute("favoriteid"));
<table>
  <td class="favoriteRow" style="">
    <img src="/images/starGold.png" loading="lazy" favoriteid="bitcoin">
  </td>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2013-02-19
    • 1970-01-01
    相关资源
    最近更新 更多