简体   繁体   English

使用jQuery的.load()加载页面

[英]Load page using jQuery's .load()

After trying to get this to work for hours I turn to you guys. 试图让这个工作几个小时后,我转向你们。

I want to load a page into a div tag using jQuery. 我想使用jQuery将页面加载到div标签中。 The page I'm loading has a HTML5 media player: 我正在加载的页面有一个HTML5媒体播放器:

<h3> {{ title }} </h3>
<audio controls autoplay>
    <source src="/static/media/{{ song }}" type="audio/mp3">
    Your browser does not support this audio format.
</audio>

when I load the page directly it works fine. 当我直接加载页面时它工作正常。 When I do it like this: 当我这样做时:

<script type="text/javascript">
$(function() {

    $( ".song" ).click(function() {
        alert('hello')
    });

    $("#mp").load("/playsong?song_id=53847b72936aa27d640039f7");

});
</script>

It works fine (and the hello message works!) 它工作正常(并且hello消息有效!)

But when I put the load command inside the .click() event, it does not work: 但是当我将load命令放在.click()事件中时,它不起作用:

<script type="text/javascript">
$(function() {

    $( ".song" ).click(function() {
        alert('hello')
        $("#mp").load("/playsong?song_id=53847b72936aa27d640039f7");
    });

});
</script>

I am using flask on the backend and in all cases the code on the backend gets executed. 我在后端使用flask,在所有情况下,后端的代码都会被执行。

try this: 尝试这个:

<script type="text/javascript">
    $(function() {

        $(document).on("load","#mp","/playsong?song_id=53847b72936aa27d640039f7");
        $( ".song" ).click(function() {
            alert('hello')
            $( "#mp" ).trigger( "load" );
        });

    });
</script>

let me know if this works, 'cause I have a backup answer! 让我知道这是否有效,因为我有一个备用答案!

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

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