Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery在子窗口中不工作?_Jquery_Window_Cross Window Scripting - Fatal编程技术网

jQuery在子窗口中不工作?

jQuery在子窗口中不工作?,jquery,window,cross-window-scripting,Jquery,Window,Cross Window Scripting,当我写作时: var x = window.open('','','width=480,height=500'); x.document.write(' <html> <head> <link rel="stylesheet" type="text/css" href="chat.css" / > <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery

当我写作时:

var x = window.open('','','width=480,height=500');
x.document.write('
<html>
<head>
<link rel="stylesheet" type="text/css" href="chat.css" / >
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"><' +'/script>
<script type="text/javascript">
alert("hi");
<' + '/script>
</head>
<body><p>hi</p></body>
</html>');
var x=window.open(“”,,'width=480,height=500');
x、 文件。写('
警报(“hi”);
嗨

');
我收到警报

但如果我这样做了--

x.document.write('
$(函数(){
警报(“hi”);
});
嗨

');

那就不行了。它什么也不做。我整晚都在想这个问题。有什么想法吗?

这是因为jQuery从不触发DOM就绪事件,除非您正确地
。在写入文档后关闭()

var x = window.open('','','width=480,height=500'),
x.document.write('all your html and script here');
x.document.close();

应得积分:

这是因为除非正确地
.close()
写入文档后,jQuery不会触发DOM就绪事件:

var x = window.open('','','width=480,height=500'),
x.document.write('all your html and script here');
x.document.close();

应付账款:

哦,jk。顺便说一句,我只是为了可读性而格式化了它——实际上并不是这样写的。发布时不要更改代码。在任何情况下,我都在JSFIDLE中尝试过这段代码,效果很好。@Aerovistae用fiddle linkWell更新了答案,如果我不把这几行分开,那么你在阅读这段代码时会非常痛苦。无论如何,如上所述,行分隔不是问题所在——我的实际代码只是将它放在一个大的块中。不过还是谢谢你。@Aerovistae我明白了,你查过小提琴了吗?哦,jk。顺便说一句,我只是为了可读性而格式化了它——实际上并不是这样写的。发布时不要更改代码。在任何情况下,我都在JSFIDLE中尝试过这段代码,效果很好。@Aerovistae用fiddle linkWell更新了答案,如果我不把这几行分开,那么你在阅读这段代码时会非常痛苦。无论如何,如上所述,行分隔不是问题所在——我的实际代码只是将它放在一个大的块中。“不过还是谢谢你。”Aerovistae我明白了,你查过小提琴了吗?