发布时间:2026-02-04 21:19:05 浏览次数:1
<script language="javascript" type="text/javascript">function playSound(soundfile) {document.getElementById("dummy").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\"loop=\"false\" />";}</script> 将声音放置在空跨度中当脚本启动时,JavaScript将一个Embed元素放在一个空的span元素中。因此,您需要将以下SPAN标记添加到HTML页面正文中的某个位置,最好是在文档顶部附近:<span ></span>使用属性调用脚本您需要添加的最后一件事是一个元素,您希望在单击或鼠标悬停时生成声音。使用以下属性之一调用脚本。将UrlToSoundFile替换为要播放的声音文件的完整URL:
<a href="#"onclick="playSound('UrlToSoundFile');">Click here to hear a sound</a><ponmouseover="playSound('UrlToSoundFile');">Mouse over this text to hear a sound</p>这是整个HTML文档,播放的是蓝调的声音。声音文件存储在HTML页面所在的目录中:<!doctype html><html><head><meta charset="ISO-8859-1" /><title>Example of How to Play a Sound on Click or on MouseOver</title><script language="javascript" type="text/javascript"> function playSound(soundfile) { document.getElementById("dummy").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; } </script> </head><body><span ></span> <p><a href="#"onclick="playSound('zbluejay.wav');">Click here to hear a bird sing</a></p><ponmouseover="playSound('zbluejay.wav');">Or you can put your mouse over this paragraph to hear the same bird sound.</p></body></html>