有没有办法在document.ready之后允许document.write?

时间:2015-01-08 15:58:58

标签: javascript jquery

我尝试重新定义document.ready函数来捕获document.ready之后应该写的内容。已经

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

我现在有HTML应该呈现,但我不知道它应该属于哪个。

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

1 个答案:

答案 0 :(得分:1)

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.

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

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

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() will replace the entire innner content, and .append() will add to the element after the other content.