单击图像时播放声音文件 [英] Play sound file when image is clicked

查看:149
本文介绍了单击图像时播放声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是原生的 HTML 程序员,所以请不要跳过我这个简单的问题。



 < img name =track1src我有一张图片,我正在使用以下代码进行显示:

=images / track1.pngwidth =180height =180border =0id =track1alt =/>

我想要在点击图片时播放声音文件。我可以使图像成为一个按钮,但是由于某种原因,这会让页面的布局变得糟糕。



我可以使用任何播放器,但唯一的是我不想展示侵入性球员。我只想让用户按下图像并听到音乐。如果他按下另一张图像,则当前音乐需要停止播放,并且必须播放不同的声音。



请帮忙!感谢! 首先,您必须使用 jQuery的
例如,您可以在您的页面中创建一些< div> ;



< div id =wrap>& nbsp;< / div> p>

然后,在您的JavaScript中,当您想要播放文件时,只需添加

 <$ c $'>'('#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">&nbsp;</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">&nbsp;</div>

Hope this helps.

这篇关于单击图像时播放声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆