有没有一种方法可以在document.ready之后允许document.write? [英] Is there a way to allow document.write after document.ready?

查看:68
本文介绍了有没有一种方法可以在document.ready之后允许document.write?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重新定义document.ready函数以捕获应该在document.ready之后写的内容

I tried to redefine the document.ready function to capture what should have been written after document.ready

$(document).ready(function(){
 document.write = function(html){
    //... do something with HTML
 };
});

我现在确实具有应该呈现的HTML,但是我不知道它应该属于哪里.

I do have the HTML now which should be rendered, but I don't know where it should belong to.

是否有办法找出哪个脚本称为document.write函数(以便将HTML代码放置在正确的位置)?

Is there a way to find out which script called the document.write function (in order to place the HTML code in the right place)?

推荐答案

在这种情况下,您无需使用document.write.您可以使用jquery选择器和.html()方法(或.append()或类似方法)将内容放置在元素内.

You don't need to use document.write in this situation. You use a jquery selector and the .html() method (or .append() or similar) to place content inside an element.

例如,如果您的div类别为container

For example, if you had a div with the class container

var content = '<p>Some content</p>';
$('div.container').html(content);

此代码会将容器的内容替换为content的值.您会注意到CSS样式选择器用于选择元素,因此要选择整个主体,您可以使用

this code would replace the contents of the container with the value of content. You'll notice that CSS style selectors are used for selecting elements, so to select the entire body you can use

$('body')

或对于所有文本输入,您可以使用

or for all text inputs, you can use

$('input[type="text"]')

.html()将替换整个内部内容,而.append()将添加到元素的其他内容之后.

.html() will replace the entire innner content, and .append() will add to the element after the other content.

这篇关于有没有一种方法可以在document.ready之后允许document.write?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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