小编典典

触发$ document.ready(因此执行了我无法修改的AJAX代码)

ajax

我的要求如下:

  • 我有一个丰富的网页,该网页有时会div通过AJAX 将HTML加载到其中。
  • 我检索的HTML确实有javascript(<script>...</script>
  • 检索到的javascript包含$('document').ready( ... )部分
  • 无法 修改检索到的javascript;它来自外部库
  • 我已经加载AJAX时调用了一个javascript函数。我试图通过执行以下操作来“欺骗”它:
    function AjaxLoaded() {
    

    $(‘document’).trigger(‘ready’);
    }

恐怕这并没有解决。

我已经看到了一些 关于响应,它们通过更改AJAX上返回的代码来“规避”这个问题(使其成为函数,并在加载后调用它,或者只是删除$(document).ready())。我需要强调的是,在这种情况下,我无法更改检索到的代码。


阅读 320

收藏
2020-07-26

共1个答案

小编典典

经过一些研究,我创造了一种使其工作的方法。

这是我的测试,表明它可以正常工作:http :
//www.antiyes.com/test/test2.php

这是相关代码:

<script>
    // easy copy of an array
    Array.prototype.copy = function() {
        return [].concat(this);
    };

    // this function is added to jQuery, it allows access to the readylist
    // it works for jQuery 1.3.2, it might break on future versions
    $.getReadyList = function() {
        if(this.readyList != null)
            this.myreadylist =  this.readyList.copy();      
        return this.myreadylist;
    };

    $(document).ready(function() {
        alert("blah");
    });

</script>

<script>

    // this should be added last so it gets all the ready event
    $(document).ready(function() {
        readylist = $.getReadyList();
    });

</script>

然后在体内我有:

<input type="button" onclick="$(readylist).each(function(){this();});" value="trigger ready" />

基本上,我所做的就是在jQuery中添加了一个函数,该函数在清除readyList之前将其复制出来,然后可供您使用。

看起来下面的代码不起作用:

function AjaxLoaded() {
    $(document).trigger('ready');
}

删除报价document

2020-07-26