【发布时间】:2017-08-18 19:17:05
【问题描述】:
我有这三个功能,我也想应用 DRY 原则。
JS
function shakeBell(){
document.getElementById('shakeBell').play();
}
function shakeShake() {
document.getElementById('shakeShake').play();
}
function blowWhistle(){
document.getElementById('blowWhistle').play();
}
HTML
<audio id="shakeBell" src="audio/bell-ringing-02.mp3" preload="auto"></audio>
<audio id="shakeShake" src="audio/pill-bottle-1.mp3" preload="auto"></audio>
<audio id="blowWhistle" src="whistle-flute-2.mp3" preload="auto"></audio>
<div class="imagelist">
<a href="javascript:shakeBell();">
<img src="images/Golden_Bell.png" style="width:100px; height 100px;"></a>
<img src="images/Maracas.png" style="width:100px; height:100px;">
</div>
我有多个图像,单击它们会播放该图像的特定声音文件。如果不为每张图片制作单独的功能,我该如何做到这一点?
【问题讨论】:
-
为什么不将元素的 id 作为参数传递给您的函数?
标签: javascript html dry