【问题标题】:onclick not working with html5onclick 不适用于 html5
【发布时间】:2014-08-07 01:41:18
【问题描述】:

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

<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>

【问题讨论】:

    标签: jquery html css iframe


    【解决方案1】:

    此示例使用数据属性并与 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>
    

    【讨论】:

      【解决方案2】:

      为什么你使用单独的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>
      

      此外,如果您想同时使用onclickhref,请尝试

      <a href="javascript: void(0);" onClick='playSound(this, "sliderBeatles/engine1/dil.mp3");'>
      

      请注意 onclick 事件的单引号 '。 谢谢

      【讨论】:

      • 对我来说,在单独使用 href 和单引号时都有效。控制台有什么错误???
      • 我也清除了缓存,我使用的是 mozilla firefox,当我点击播放声音按钮时没有任何反应。
      • 看起来按钮和脚本可能位于不同的文档中。您能否确认一下,按钮或脚本是否在 iframe 中??
      【解决方案3】:

      对不起,伙计,我认为您在 onclick 上犯了一个小错误,我认为如果您将其写成 onClick 并在大写字母上带有“C”,它将被视为您定义的属性。

      你试过写onclick="playSound(...)" insted of onClick="playSound(..)" 吗?

      我认为这可能是问题所在,谁知道...也许:)

      希望对你有帮助!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-17
        • 2013-05-26
        • 1970-01-01
        • 2013-01-26
        • 2013-05-24
        • 2016-04-01
        • 1970-01-01
        • 2019-02-03
        相关资源
        最近更新 更多