【问题标题】:closing a window pop up on button click opened using window.open()关闭使用 window.open() 打开的按钮单击时弹出的窗口
【发布时间】:2022-11-13 18:11:44
【问题描述】:

当用户单击打开按钮时,我想创建另一个窗口。如果是 div,则此窗口将包含内容。它还会有一个关闭按钮。单击关闭按钮时,我想关闭此弹出窗口。

当我们点击打开时,它会打开<div id="myContent">
在另一个窗口中,但在打开它之后,我希望它使用关闭按钮关闭该窗口。

document.getElementById("mybtn").addEventListener("click", function() {
  var newWindowContent = document.getElementById("myContent").innerHTML;
  var newWindow = window.open("", "", "width=500,height=400");
  newWindow.document.write(newWindowContent);
});
<article type="text" id="editor" contenteditable="true">
  Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatum ad eos, facere ipsam odio dignissimos voluptatibus dolor sunt qui ipsum, quos praesentium doloribus nulla mollitia laboriosam expedita maxime, asperiores consequuntur?
</article>
<h1>Open the element content when click the button</h1>
<div id="myContent">
  <button id="mybtnn">close</button> This is the content that will be inside the new popup window
</div>
<button id="mybtn">open</button>

【问题讨论】:

  • 只需在单击按钮时使用 window.close() 即可。此外,您需要在完成写入后关闭文档newWindow.document.write(newWindowContent); newWindow.document.close();,但为什么不在模态对话框中显示一个新的 div,那么您不必担心浏览器不允许新窗口

标签: javascript html jquery google-chrome browser


【解决方案1】:

您可以使用window.close

<button id="mybtnn" onclick="window.close()">close</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多