jQuery $(document).ready()和document.write() [英] JQuery $(document).ready() and document.write()

查看:99
本文介绍了jQuery $(document).ready()和document.write()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,有没有一种方法可以在JQuery的$(document).ready()方法中使用document.write()?如果有,请提示我,因为这将解决我的问题.

Firstly, is there a way to use document.write() inside of JQuery's $(document).ready() method? If there is, please clue me in because that will resolve my issue.

否则,我有应该与我一起工作的某人的代码.问题是我不允许以任何方式更改他的代码.无效的部分看起来像这样:

Otherwise, I have someone's code that I'm supposed to make work with mine. The catch is that I am not allowed to alter his code in any way. The part that doesn't work looks something like this:

document.write('<script src=\"http://myurl.com/page.aspx?id=1\"></script>');

script标记引用了一个进行了一系列测试的aspx页面,然后吐出了类似的内容:

The script tag is referencing an aspx page that does a series of tests and then spits out something like so:

document.write('<img src=\"/image/1.jpg\" alt=\"Second image for id 1\">')

这些脚本只是实际发生情况的示例.这里的问题是,我在初始脚本中有一个document.write(),在脚本中有一个document.write()追加到第一个脚本之后,我必须以某种方式使它在JQuery的$( document).ready()函数,而无需更改其代码.

The scripts are just examples of what is actually going on. The problem here is that I've got a document.write() in the initial script and a document.write() in the script that get's appended to the first script and I've got to somehow make this work within JQuery's $(document).ready() function, without changing his code.

我不知道该怎么办.帮助吗?

I have no idea what to do. Help?

推荐答案

在满足要求的情况下,不,您不能在没有真正打开文档的情况下使用document.write.如果您确实想不更改代码,则可以像这样覆盖document.write()的功能,并在以后添加结果:

With the requirements given, no, you can't use document.write without really hosing up the document. If you're really bent on not changing the code, you can override the functionality of document.write() like so and tack on the result later:

var phbRequirement = "";

$(function() {
  document.write = function(evil) {
    phbRequirement += evil;
  }
  document.write("Haha, you can't change my code!");
  $('body').append(phbRequirement);

});

请确保在使用document.write函数之前将其覆盖.您可以随时做到.

Make sure you overwrite the document.write function before it is used. You can do it at anytime.

其他答案很无聊,这很有趣,但是为了满足给定的要求,这样做的方式非常错误.

The other answers are boring, this is fun, but very pretty much doing it the wrong way for the sake of fulfilling the requirements given.

这篇关于jQuery $(document).ready()和document.write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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