我不是原生的 HTML 程序员,所以请不要跳过我这个简单的问题。
< img name =track1src我有一张图片,我正在使用以下代码进行显示: =images / track1.pngwidth =180height =180border =0id =track1alt =/>我想要在点击图片时播放声音文件。我可以使图像成为一个按钮,但是由于某种原因,这会让页面的布局变得糟糕。
我可以使用任何播放器,但唯一的是我不想展示侵入性球员。我只想让用户按下图像并听到音乐。如果他按下另一张图像,则当前音乐需要停止播放,并且必须播放不同的声音。
请帮忙!感谢! 首先,您必须使用 jQuery的。 例如,您可以在您的页面中创建一些< div> ;
< div id =wrap>& nbsp;< / div> p>
然后,在您的JavaScript中,当您想要播放文件时,只需添加
'('#wrap')。append('< embed id =embed_playersrc =audio.wavautostart =truehidden =true>< / embed>');整个代码看起来像这样;
< script type =text / javascriptsrc =jquery-1.7.1.js>< / script> < script type =text / javascript> $(document).ready(function(){ $('#track1')。click(function(){ $('#wrap')。append('< embed id =embed_playersrc =audio.wavautostart =truehidden =true>< / embed>'); }); }); < / script> < img name =track1src =images / track1.pngwidth =180height =180border =0id =track1alt =/> < div id =wrap>& nbsp;< / div>希望这有助于您。
I am not a native HTML programmer, so please don't jump all over me about this simple question.
I have an image that, I am displaying using the following code:
<img name="track1" src="images/track1.png" width="180" height="180" border="0" id="track1" alt="" />I want a sound file to be played when that image is clicked. I can make the image a button, but that is messing up the layout of the page for some reason.
I am okay with using any player, but the only thing is that I do not want to display an intrusive player. I just want the user to press the image and hear the music. If he presses another image, the current music needs to stop playing, and a different sound must be played.
Please help! Thanks!
解决方案First you have to use jQuery. You may create some <div> in your page having some id, for example;
<div id="wrap"> </div>.
Then, in your JavaScript, when you want to play the file, just add
$('#wrap').append('<embed id="embed_player" src="audio.wav" autostart="true" hidden="true"></embed>');the whole code looks something like;
<script type="text/javascript" src="jquery-1.7.1.js"></script> <script type="text/javascript"> $(document).ready( function() { $('#track1').click(function(){ $('#wrap').append('<embed id="embed_player" src="audio.wav" autostart="true" hidden="true"></embed>'); }); }); </script> <img name="track1" src="images/track1.png" width="180" height="180" border="0" id="track1" alt="" /> <div id="wrap"> </div>Hope this helps.
更多推荐
单击图像时播放声音文件
发布评论