dongqiu3709 2014-11-27 09:19
浏览 71

脚本在AJAX加载的DIV中不起作用,只加载了html?

My JavaScript is not working after ajax response i know that i have to use $('.ajax-link').on('click', 'a', function (e){ function for it but when i am using this mye.preventDefault(); is not working (means whole page is loading instead of ajax call) and if i am using $('a.ajax-link').click(function (e) { then ajax is working fine but all script of ajax response page is not working after ajax response . i am using jquery 1.10. below is my whole code :

$('a.ajax-link').click(function (e) {
        NProgress.start();
        if (msie) e.which = 1;
        if (e.which != 1 || !$('#is-ajax').prop('checked') || $(this).parent().hasClass('active')) return;
        e.preventDefault();
        $('.sidebar-nav').removeClass('active');
        $('.navbar-toggle').removeClass('active');
        $('#loading').remove();
        $('#dvLoading').show();
        var $clink = $(this);
        History.pushState(null, null, $clink.attr('href'));
        $('ul li.active').removeClass('active');
        $clink.parent('li').addClass('active');
    });

    History.Adapter.bind(window, 'statechange', function () { // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state   



    $.ajax({
        url: State.url,
        success: function (msg) {
           $('#dvLoading').fadeOut(10);
            NProgress.inc();
            $('.main-sec').html($(msg).find('.main-sec').html());
             NProgress.inc();
            setTimeout(function() { NProgress.done(); $('.fade').removeClass('out'); }, 100);
            $('#loading').remove();
            $('.main-sec').fadeIn();
            var newTitle = $(msg).filter('title').text();
            $('title').text(newTitle);
            docReady();
        }
    });
});
  • 写回答

1条回答 默认 最新

  • doutuobao4004 2014-11-27 09:56
    关注

    You can append manually the scripts tag to your document :

    $.ajax({
        url: State.url,
        success: function (msg) {
          var scripts = $("<div>").html(msg).find( "script");
          scripts.each(function(){
              $('body').append($(this)[0]);
            }
        }
    });
    

    or you could use the AMD pattern, take a look at requirejs: http://requirejs.org/

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?