触发$的document.ready(所以AJAX code我不能修改执行) [英] Trigger $document.ready (so AJAX code I can't modify is executed)

查看:157
本文介绍了触发$的document.ready(所以AJAX code我不能修改执行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求如下:

  • 在我有一个丰富的网页,在某一时刻加载在 DIV ,通过AJAX。
  • 一堆的HTML
  • 在我检索HTML确实有JavaScript的(<脚本> ...< / SCRIPT>
  • 在检索到的JavaScript包含 $('文件')。准备好(...)部分
  • 我不能修改检索到的JavaScript;它来自外部的lib
  • 我有一个JavaScript函数的AJAX加载时被调用。我试图诱骗到做执行:

  • I've got a rich webpage that at a certain moment loads a bunch of HTML in a div, via AJAX.
  • The HTML I retrieve does have javascript (<script>...</script>)
  • The retrieved javascript contains $('document').ready( ... ) parts
  • I can not modify the retrieved javascript; it comes from an external lib
  • I've got a javascript function that is called when the AJAX is loaded. I'm trying to "trick it" into executing by doing:

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

这不剪,我很害怕。

我见过几个 <一href="http://stackoverflow.com/questions/2200540/document-ready-not-executed-for-ajax-loaded-content">responses对堆栈溢出说,回避这个问题,通过更改AJAX返回的(使其成为一个功能和加载后调用它,或者只是删除 $(文件)。就绪(在code ))。我要强调指出,我不能改变检索code在这种情况下。

I've seen several responses on Stack Overflow that "evade" this question by changing the code that is returned on the AJAX (make it a function and call it after loading, or just remove the $(document).ready()). I need to stress out that I can't change the retrieved code on this case.

推荐答案

AFER一些研究,我创建了一个方法来得到它的工作。

Afer some research i created a way to get it to work.

下面是我的测试显示它的工作: http://www.antiyes.com/test/test2.php

here is my test that shows it working: http://www.antiyes.com/test/test2.php

这里是有关code:

<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>

然后在身体我有:

then in the body I have:

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

基本上我所做的就是一个函数添加到jQuery的该份readyList之前它清除出去,那么这将是可使用由你。

basically what i did was add a function to jQuery that copies the readyList before it's cleared out, then it will be available to be used by you.

它看起来像下面不工作的code:

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

串门文档引号

这篇关于触发$的document.ready(所以AJAX code我不能修改执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆