gpt4 book ai didi

javascript - 访问特定 html 元素值而不更改 HTML

转载 作者:行者123 更新时间:2023-12-02 19:27:51 24 4
gpt4 key购买 nike

我是 Javasript 的新手,想了解如何访问特定元素。我有超过 20 首歌曲内容。如果我已经在脚本中的变量中拥有了跨度 ID,例如“歌曲 3”,那么如何访问与该歌曲相关的“ProducedBy”类,更具体地说,其中包含的文本在本例中表示“无论天气”。不改变底层 HTML?

非常感谢任何有助于理解 JavaScript 的帮助。谢谢。

    <div class="Artist Song">
<span id="song_1" class="song">
<span class="artistname">MJ</span>
<span class="songname">Billie Jean</span>
<span class="email" style="whatever"><strong>neverland@ranch.com</strong></span>
<span class="recordlabel">def jam</span>
<span class="age">34 years</span>
<span class="album">Album:<strong>Bad</strong></span>
<span class="dateReleased" style="display:none">whatever</span>
<span class="producedBy" style="display:none">Produced by Quincy jones</span>
</span>
</div>

<div class="Artist Song">
<span id="song_2" class="song">
<span class="artistname">Prodigy</span>
<span class="songname">Firestarter</span>
<span class="email" style="whatever"><strong>firestarter@twisted.com</strong></span>
<span class="recordlabel">whatever</span>
<span class="age">whatever</span>
<span class="album">Album:<strong>Fat of the land</strong></span>
<span class="dateReleased" style="display:none">whatever</span>
<span class="producedBy" style="display:none">whatever</span>
</span>
</div>

<div class="Artist Song">
<span id="song_3" class="song">
<span class="artistname">Whoever</span>
<span class="songname">Whatever</span>
<span class="email" style="whatever"><strong>whatever@wherever.com</strong></span>
<span class="recordlabel">whatever</span>
<span class="age">whatever</span>
<span class="album">Album:<strong>Whatever</strong></span>
<span class="dateReleased" style="display:none">whatever</span>
<span class="producedBy" style="display:none">whatever the weather</span>
</span>
</div>

<div class="Artist Song">
<span id="song_4" class="song">
<span class="artistname">Whoever</span>
<span class="songname">Whatever</span>
<span class="email" style="whatever"><strong>whatever@wherever.com</strong></span>
<span class="recordlabel">whatever</span>
<span class="age">whatever</span>
<span class="album">Album:<strong>Whatever</strong></span>
<span class="dateReleased" style="display:none">whatever</span>
<span class="producedBy" style="display:none">whatever</span>
</span>
</div>

...

最佳答案

document.getElementById("song_1").getElementsByClassName("producedBy")[0]

关于javascript - 访问特定 html 元素值而不更改 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11836549/

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