【问题标题】:Input onClick works on Chrome, FF, IE but not Safari, BUT does work on Safari locally?输入 onClick 适用于 Chrome、FF、IE 但不适用于 Safari,但是否适用于本地 Safari?
【发布时间】:2023-04-05 06:02:01
【问题描述】:

我以为我有一个非常简单的 onclick 输入和脚本来播放随机音频文件。

一切都在我的系统上本地运行,没有问题。在线代码适用于 Chrome、Firefox 和 IE,但不适用于 Safari。由于它在 Safari 上本地运行,我正在努力寻找问题,并想知道是否有人能指出我正确的方向? Safari 的 Web Inspector 也没有错误记录!

<input class="btn btn-primary showPointer" type="button" onclick="getRandomSounds()" value="Play A Clip" />

<script type="text/javascript">
  var sounds = new Array();
  sounds[0] = "example.mp3";
  sounds[1] = "example1.mp3";

  function getRandomSounds() {
    var randomNum = Math.floor(Math.random()*sounds.length);
    document.getElementById("ashboxplayer").src=sounds[randomNum];
  }
</script>

在这个例子中,数组有所缩短,但数组中有 80 个对象。

提前感谢任何人都可以提供的任何启示!

干杯

【问题讨论】:

  • 请定义“不起作用”。
  • 尝试使用 onfocus 代替,可能有效,没有测试
  • 试过 onfocus,没有运气...
  • @Teemu 在 Safari 中单击按钮后没有任何反应! (在 Chrome、FF、IE 中很好,在本地 Safari 上也很好)。您可以在这里亲自查看:www.ashgould.com(它是“ashbox”)
  • 什么不起作用?如果您将函数更改为console.logalert 怎么办?是播放声音的问题还是真的与点击事件有关?如果使用 HTML 而不是 JavaScript 设置 &lt;audio&gt; 元素的 src 会发生什么?

标签: javascript html safari


【解决方案1】:

想通了。

Safari 有内容拦截器和自动播放拦截器。网站禁用后,一切正常。

感谢大家的帮助!

【讨论】:

    【解决方案2】:

    试试这个

        <input class="btn btn-primary showPointer" type="button" id="randombtn"  value="Play A Clip" />
         var sounds = new Array();
          sounds[0] = "example.mp3";
          sounds[1] = "example1.mp3";
        $('#randombtn).on('click',function(){
       var randomNum = Math.floor(Math.random()*sounds.length);
            document.getElementById("ashboxplayer").src=sounds[randomNum];
    
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2014-08-28
      • 2015-09-16
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多