jplayer multiple mp3 file links in one page

前端 未结 4 497
旧时难觅i
旧时难觅i 2021-01-16 00:15

I use jplayer in my page and when clicked a link I want to play the clicked one. However every time test1.mp3 is played. How can I solve it? The code is below:

the p

4条回答
  •  清酒与你
    2021-01-16 00:57

    HTML:

    Song1
    Song1
    Song1
    

    jQuery:

    $(function () {
        $('.ChangePath').on('click', function () {
    
        $("#jquery_jplayer_1").jPlayer("destroy");
    
        var link = "test" + $(this).data('key') + ".mp3";
    
        $("#jquery_jplayer_1").jPlayer({
            ready: function () {
                $(this).jPlayer("setMedia", {
                    mp3: link
                });
            },
            swfPath: "~/Scripts/jplayer",
            supplied: "mp3"
        });
    
        player.jPlayer("play", 0);
    
    });
    });
    

    If you are using ajax:

    $(function () {
        $('.ChangePath').on('click', function () {
       $.ajax({
        $("#jquery_jplayer_1").jPlayer("destroy");
    
        var link = "test" + $(this).data('key') + ".mp3";
    
        $("#jquery_jplayer_1").jPlayer({
            ready: function () {
                $(this).jPlayer("setMedia", {
                    mp3: link
                });
            },
            swfPath: "~/Scripts/jplayer",
            supplied: "mp3"
        });
    
        player.jPlayer("play", 0);
    });
    });
    });
    

    Depending on your project you might need to change the hyperlinks to something else, but the jQuery will work.

提交回复
热议问题