简体   繁体   English

$(单击链接时不执行-每次都需要重新加载页面

[英]$( 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. 我正在使用Jquery Mobile开发Web应用程序,但遇到了问题。 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. 我的问题是,当用户单击链接(未加载Ajax)时,似乎不会执行$(function() {而仅在手动刷新页面时才会执行。

I also tried <body onload=" as well as quick ans direty writing the code directly into the DOM... Nothing changed the issue... 我还尝试了<body onload="以及快速将代码直接写入DOM的烦恼...没有什么改变了这个问题...

猜猜我发现了问题...这是我完全错过的集成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: 例如,单击“ a”将触发foo函数:

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

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

Hope it helps ! 希望能帮助到你 !

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

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