gpt4 book ai didi

javascript - 如何获取 元素内的属性值?

转载 作者:行者123 更新时间:2023-11-28 15:08:46 26 4
gpt4 key购买 nike

我正在尝试从 JS 游戏的控制台提取游戏分数。

document.getElementById("game").getElementsByClassName("game-score")

这将返回包含分数的范围,即:

<span class="game-score" score="550"></span>

我正在尝试提取分数元素,因此我只想返回 550。有没有简单的方法可以做到这一点?

最佳答案

使用getAttribute()

document.getElementById("game").getElementsByClassName("game-score")[0].getAttribute("score")

console.log(document.getElementById("game").getElementsByClassName("game-score")[0].getAttribute("score"))
<div id="game">
<span class="game-score" score="550"></span>
</div>

关于javascript - 如何获取 <span> 元素内的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com