jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery ajax调用点击,只能工作一次大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个简单的 jquery代码.点击它获取标签的URL,在当前内容旁边加载该页面,滑动它并删除内容.
页面的状态与以前完全相同,相同的元素没有额外的类或样式.
问题是下一个ajax@L_772_11@不起作用.也许我需要.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,footer').css("-webkit-transform","translate(" + sm + "px,0pX)");
                };
                if(direction != "leftnav"){
                    $('body>header:last,sm + "px");
                    footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true) ;
                    $('footer:last').css("top","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>

解决方法

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

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

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

});

大佬总结

以上是大佬教程为你收集整理的jquery ajax调用点击,只能工作一次全部内容,希望文章能够帮你解决jquery ajax调用点击,只能工作一次所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。