jquery ajax 单击时调用,仅有效一次

标签 jquery ajax

我有这个简单的 jquery 代码。单击时,它会获取标签的 URL,加载当前内容旁边的页面,滑动它并删除旧内容。 页面的状态与以前完全相同,相同的元素没有额外的类或样式。 问题是下一个 ajax 调用不起作用。也许我需要 .unbind() 一些东西?

我对 jquery 和 javascript 很陌生,所以我很迷茫。非常感谢您的帮助:)

<script  type="text/javascript">
    $(document).ready(function(){ 
        loadPage();
    });
    function loadPage(url) {
        if (url == undefined) {
            $('body').load('index.html header:first,#content,footer', hijackLinks);
        } else {
            $.get(url , function(data) {
                $('body').append(data);
                $('body>meta').remove();
                $('body>link').remove();
                $('body>title').remove();
                $('body').append(direction);
                sm = $(window).width();
                if(direction == "leftnav"){
                    $('body>header:last,body>#content:last,footer:last').css("left", "-" + sm + "px");
                    footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true) ;
                    $('footer:last').css("top", footerheight);
                    $('body>header,body>#content,footer').css("-webkit-transition-duration","0.5s")
                    $('body>header,body>#content,footer').css("-webkit-transform","translate(" + sm + "px,0px)");
                };
                if(direction != "leftnav"){
                    $('body>header:last,body>#content:last,footer:last').css("left", sm + "px");
                    footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true) ;
                    $('footer:last').css("top", footerheight);
                    $('body>header,body>#content,footer').css("-webkit-transition-duration","0.5s")
                    $('body>header,body>#content,footer').css("-webkit-transform","translate(-" + sm + "px,0px)");
                };
                setTimeout(function(){
                    $('body>header:not(:last),body>footer:not(:last),body>#content:not(:last)').remove()
                },500); 
                setTimeout(function() {
                    $('body>header,body>footer,body>#content').removeAttr('style') 
                },500);
            });
        }
    }
    function hijackLinks() {
        $('a').click(function(e){
            e.preventDefault();
            loadPage(e.target.href);
        direction = $(this).attr('class');
        });
    }
</script>

最佳答案

由于您正在动态加载内容,这会替换 body 的内容,因此您的事件处理程序很可能不会保留。

要解决此问题,您需要调整点击处理程序以使用 live()delegate()

$('a').live("click", function(e){
    e.preventDefault();
    loadPage(e.target.href);
    direction = $(this).attr('class');

});

关于jquery ajax 单击时调用,仅有效一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5347229/

相关文章:

jquery - Internet Explorer 通过半透明的 div 背景截取文本

javascript - 在 jQuery/javascript 中从头编写图像裁剪器和缩放器(同时)?

jquery 自动完成动态生成的文本框不起作用

javascript - 创建复杂嵌套 AJAX/jQuery 表单的模式/策略

javascript - 使 YUI TreeView 仅使用 +/- 图标展开/折叠

javascript - Wordpress + Ajax 更新记录不起作用

javascript - 文本框中的链接更改

mysql表重新设计思路(关系)

javascript - 如何将 AJAX/Jquery 对象发送到 Controller (差异)MVC4

javascript - onreadystatechange 函数在 AJAX 中不起作用