【问题标题】:JQuery click code not getting executedJQuery点击代码没有被执行
【发布时间】:2017-05-12 02:36:59
【问题描述】:

当我点击播放按钮时,警告 - '用户点击了 'foo' 必须已经显示,但那没有发生。即没有调用jquery 点击处理程序!

<html>
        <head>
          <script type = "text/javascript" src ="new.js"></script>
          <script src = "/socket.io/socket.io.js"></script>
          <script src="jquery-3.1.1.min.js"></script>
        </head>
        <body>
            <button type="button" id = "play" name = "play" value = "play">PLAY</button>
            <button onclick = "pauseAd()" type ="button" name = "pause" value = "pause">PAUSE</button>
            </br>
            <audio controls id = "song">
                <source src = "Lyrics to Swallowed in the Sea - Coldplay.mp3" type="audio/mpeg" >
            </audio>

            <script>
                var socket = io.connect("http://localhost:3000");

                $(function() {
                    $('#play').bind('click',function(){
                        alert( "User clicked on 'foo.'" );
                        socket.emit('msg', {sent : 1});
                        return false;
                    });
                });


                socket.on('msg-received', function(data) {
                    if(data.value = 1) {
                        $('#play').click();
                    }
                });
            </script>
        </body>
    </html>

【问题讨论】:

    标签: jquery html node.js


    【解决方案1】:

    如果您将其分解为您所要求的,代码可以正常工作。很可能是代码中的其他内容导致错误。

    <!-- Bare-Bones HTML CODE -->
    <button type="button" id="play" name="play" value="play">PLAY</button>
    
    // JavaScript
    $(function() {
      $('#play').bind('click', function() {
        alert("User clicked on 'foo.'");
        return false;
      });
    });
    

    您是否在浏览器的 Web 检查器或调试器中看到任何错误?

    这是工作代码的codepen(精简版)。

    【讨论】:

    • GET jquery-3.1.1.min.js 出现 404 错误但我有这个 js 文件与我上面的 html 文件位于同一目录中。
    • 如果您在 jQuery 上遇到 404 错误,则表示未加载。
    猜你喜欢
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2020-06-20
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多