Jquery onclick不使用html5

Jquery onclick不使用html5,jquery,html,css,iframe,Jquery,Html,Css,Iframe,我的代码不起作用。我在使用下面提到的代码,我在代码中使用文本而不是图像 <html> <head><script type="text/javascript"> function playSound(el,soundfile) { if (el.mp3) { if(el.mp3.paused) el.mp3.play(); else el.mp3.pause(); } else {

我的代码不起作用。我在使用下面提到的代码,我在代码中使用文本而不是图像

<html> <head><script type="text/javascript">
 function playSound(el,soundfile) {
      if (el.mp3) {
          if(el.mp3.paused) el.mp3.play();
          else el.mp3.pause();
      } else {
          el.mp3 = new Audio(soundfile);
          el.mp3.play();
      }
  }</script>
</head>
<body>
<a href="javascript: void(0);" onClick="playSound(this, 'sliderBeatles/engine1/dil.mp3');">
                            <span id="dummy">
                            Play Sound<span></a>
</body>

功能播放声音(el,声音文件){
如果(el.mp3){
如果(el.mp3.paused)el.mp3.play();
else el.mp3.pause();
}否则{
el.mp3=新音频(声音文件);
el.mp3.play();
}
}

为什么要使用seprated
onclick
事件,而
href
将为您提供技巧。使用下面的代码

<!doctype html>

<html> <head><script type="text/javascript">
 function playSound(el,soundfile) {debugger;
      if (el.mp3) {
          if(el.mp3.paused) el.mp3.play();
          else el.mp3.pause();
      } else {
          el.mp3 = new Audio(soundfile);
          el.mp3.play();
      }
  }</script>
</head>
<body>
<a href="javascript: playSound(this, 'sliderBeatles/engine1/dil.mp3');" >
                            <span id="dummy">
                            Play Sound<span></a>
</body>
</html>
请注意onclick事件的单引号
'

谢谢

此示例使用数据属性并与jQuery的.on()绑定

也许使用按钮元素更好

<html>
<head>
<script type="text/javascript">
function playSound(el,soundfile) {debugger;
  if (el.mp3) {
    if(el.mp3.paused) el.mp3.play();
    else el.mp3.pause();
  } else {
    el.mp3 = new Audio(soundfile);
    el.mp3.play();
  }
}

$('playSound').on('click', function() {
    playSound(this, $('playSound').data('song'));
});
</script>
</head>
<body>
<a id="playSound" data-song="sliderBeatles/engine1/dil.mp3">
   <span id="dummy">Play Sound<span>
</a>
</body>
</html>

函数playSound(el,soundfile){调试器;
如果(el.mp3){
如果(el.mp3.paused)el.mp3.play();
else el.mp3.pause();
}否则{
el.mp3=新音频(声音文件);
el.mp3.play();
}
}
$('playSound')。在('click',function()上{
播放声音(这个,$('playSound').data('song'));
});
播放声音

对不起,伙计,但我认为你在onclick上犯了一个小错误,我认为如果你像
onclick
那样在caps上写上“C”,它将被视为你定义的属性

您是否尝试过编写
onclick=“playSound(…)”
而不是
onclick=“playSound(…)”

我想这可能是问题所在,谁知道呢。。。也许:)


希望这有帮助

对我来说,这两种方法在单独使用href时都有效,也适用于单引号。控制台中有任何错误吗???我也清除了缓存,我使用的是mozilla firefox,当我点击播放声音按钮时,什么都没有发生。看起来按钮和脚本可能在不同的文档中。请确认一下,如果按钮或脚本在iframe内??
<html>
<head>
<script type="text/javascript">
function playSound(el,soundfile) {debugger;
  if (el.mp3) {
    if(el.mp3.paused) el.mp3.play();
    else el.mp3.pause();
  } else {
    el.mp3 = new Audio(soundfile);
    el.mp3.play();
  }
}

$('playSound').on('click', function() {
    playSound(this, $('playSound').data('song'));
});
</script>
</head>
<body>
<a id="playSound" data-song="sliderBeatles/engine1/dil.mp3">
   <span id="dummy">Play Sound<span>
</a>
</body>
</html>