gpt4 book ai didi

JavaScript 文档.querySelector()

转载 作者:行者123 更新时间:2023-11-29 19:06:39 27 4
gpt4 key购买 nike

<分区>

我是 JavaScript 的初学者,我正在关注 30 天的 javascript30.com 任务之一,第一次看到 document.querySelector()

读完这篇我很清楚 MDN - Query Selector它的作用,但是我无法弄清楚它是如何在代码中使用的,尤其是:

  • 这里的美元符号是什么意思? (我知道这不是这种语言的特定含义,这让我很困惑)

  • 一般来说,我不明白选择器括号 () 之间发生了什么,这个:

    const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);

谁能帮帮我?

谢谢!

这是构建架子鼓的代码:

function playSound(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);

if (!audio) return;
audio.currentTime = 0;
audio.play();
key.classList.add('playing'); //add class defined in css
}

function removeTransition(e) {
if (e.propertyName !== 'transform') return;
this.classList.remove('playing');
}

const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));

window.addEventListener('keydown', playSound);
<div class="keys">
<div data-key="65" class="key">
<kbd>A</kbd>
<span class="sound">clap</span>
</div>
<div data-key="83" class="key">
<kbd>S</kbd>
<span class="sound">hihat</span>
</div>
<div data-key="68" class="key">
<kbd>D</kbd>
<span class="sound">kick</span>
</div>
<div data-key="70" class="key">
<kbd>F</kbd>
<span class="sound">openhat</span>
</div>
<div data-key="71" class="key">
<kbd>G</kbd>
<span class="sound">boom</span>
</div>
<div data-key="72" class="key">
<kbd>H</kbd>
<span class="sound">ride</span>
</div>
<div data-key="74" class="key">
<kbd>J</kbd>
<span class="sound">snare</span>
</div>
<div data-key="75" class="key">
<kbd>K</kbd>
<span class="sound">tom</span>
</div>
<div data-key="76" class="key">
<kbd>L</kbd>
<span class="sound">tink</span>
</div>
</div>

<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
<audio data-key="70" src="sounds/openhat.wav"></audio>
<audio data-key="71" src="sounds/boom.wav"></audio>
<audio data-key="72" src="sounds/ride.wav"></audio>
<audio data-key="74" src="sounds/snare.wav"></audio>
<audio data-key="75" src="sounds/tom.wav"></audio>
<audio data-key="76" src="sounds/tink.wav"></audio>

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