简体   繁体   中英

$( not executed when clicking links - need to Reload page every time

I found dozens of threads about this topic but non helped so far...

I am developing a web application using Jquery Mobile and ran into an issue. I have this code snippet residing in my page:

<script type="text/javascript">
    function prepareLinks() {
        $( "a.ajax" ).on( "click", function( event ) {
            event.preventDefault();
            $.get($(this).attr( "href" ), '', function(data) {
                $("#content").html(data);
            });
        });
    }

    $(function() {
        alert('triggered');
        {if $autoload}
            $.get('{$autoload}', '', function(data) {
                $("#content").html(data);
                $("#content").trigger("create");
            });
        {/if}

        prepareLinks();
        if (screen.availWidth > 599) $( "#mainnav" ).panel( "open" );
    });
</script>

My problem is that $(function() { seems not to be executed when a user clicks a link (which is not ajax loaded) but only when the refresh the page by hand.

I also tried <body onload=" as well as quick ans direty writing the code directly into the DOM... Nothing changed the issue...

猜猜我发现了问题...这是我完全错过的集成Ajax加载功能...

It needs a trigger to actually start, as it is for the first function. For example, on click of a "a" would trigger a foo function:

var foo = function(event){
   //your code here
}
$( "a" ).on( "click", foo );

Reference: https://learn.jquery.com/events/triggering-event-handlers/

Hope it helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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