简体   繁体   English

链接仍然使用preventDefault()函数打开新窗口

[英]Link still opens new window with preventDefault() function

I am using this simple audio player for a music website. 我正在将这个简单的音频播放器用于音乐网站。 It works just fine locally but not when hosted on the website. 它在本地工作得很好,但在网站上托管时却没有。

Installed on the website the program still opens a new window when I click the other song. 当我点击另一首歌时,安装在网站上的程序仍会打开一个新窗口。 I am only a little experienced with JS and jquery and have tried changing e. 我对JS和jquery只有一点经验,并尝试过改变e。 to event. 参加活动。 but that did not work. 但那没用。 The JS is in script tags in the body, however, and not linked separately. 然而,JS是在正文中的脚本标记中,而不是单独链接。 Where do I put a console.log() function to test to see if the preventDefault() function runs? 我在哪里放置一个console.log()函数来测试preventDefault()函数是否运行?

Thank you in advance for any help. 预先感谢您的任何帮助。

Here is the script I am using for the audio player 这是我用于音频播放器的脚本

<script src="https://code.jquery.com/jquery-2.2.0.js"></script>
<script type="text/javascript>
 function audioPlayer(){
        var currentSong = 0;
        $("#audioPlayer-lk")[0].src = $("#playlist-lk li a")[0];
        $("#audioPlayer-lk")[0].play();
        $("#playlist-lk li a").click(function(e){
           e.preventDefault(); 
           $("#audioPlayer-lk")[0].src = this;
           $("#audioPlayer-lk")[0].play();
           $("#playlist-lk li").removeClass("current-song-lk");
            currentSong = $(this).parent().index();
            $(this).parent().addClass("current-song-lk");
        });

        $("#audioPlayer-lk")[0].addEventListener("ended", function(){
           currentSong++;
            if(currentSong == $("#playlist-lk li a").length)
                currentSong = 0;
            $("#playlist-lk li").removeClass("current-song-lk");
            $("#playlist-lk li:eq("+currentSong+")").addClass("current-song-lk");
            $("#audioPlayer-lk")[0].src = $("#playlist li a")[currentSong].href;
            $("#audioPlayer-lk")[0].play();
        });
    }

  audioPlayer(); </script>

You need to wrap audioPlayer() function with $(document).ready() function like below: 您需要使用$(document).ready()函数包装audioPlayer()函数,如下所示:

$(document).ready(function(){

audioPlayer();

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM