Jquery包装方法无法与window.open一起使用

Jquery wrapper method not working with window.open

本文关键字:open 一起 window 包装 方法 Jquery      更新时间:2023-09-26

我很困惑…

function myFunction ()
{
    windowOne = window.open('', "_blank", "toolbar=0, scrollbars=yes,resizable=yes,top=50,left=150");
    $("<div id='textZone' contenteditable='true' style='background-color:navajowhite; color: red; height: 40px; width:140px;'></div>").appendTo(windowOne.document.body);
    $('#textZone').html('Hurray');
}

textZone的html设置不起作用,同时在打开和关闭<div>标签之间插入html文本,如:

function myFunction ()
{
    windowOne = window.open('', "_blank", "toolbar=0, scrollbars=yes,resizable=yes,top=50,left=150");
    $("<div id='textZone' contenteditable='true' style='background-color:navajowhite; color: red; height: 40px; width:140px;'>Hurray</div>").appendTo(windowOne.document.body);
}

有人能解释一下原因吗?

编辑-到目前为止我已经尝试过:

windowOne.focus();
$('#textZone').html('Hurray');

以及:

$('body#textZone').html('Hurray');

以及:

$('windowOne.document.body').html('Hurray');

等等。

注意:我可能不得不把"Hurray"变成"Crumbs"…

正如@rdubya所说,您需要指定在其他文档中搜索元素,否则它将在主窗口中搜索元素。

试试这个:

$( '#textZone', windowOne.document.body ).html( 'Hurray' )

工作JSFiddle

您基本上需要将新元素标记写入到新窗口的引用中。

  • 定义对"弹出窗口"的引用
  • 存储对HTML的引用将存在于您的弹出窗口中
  • 调用您的弹出窗口参考,并将您存储的HTML写入该弹出窗口

function myFunction() {
  var newWindow = window.open("", "_blank", "toolbar=0, scrollbars=yes,resizable=yes,top=50,left=150");
  var newElement = "<div id='textZone' contenteditable='true' style='background-color:navajowhite; color: red; height: 40px; width:140px;'>Hurray</div>";
  newWindow.document.write(newElement);
}
myFunction();