javascript 如何使用 window.showModalDialog() 显示 HTML 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12036329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:07:40  来源:igfitidea点击:

How to display HTML Content using window.showModalDialog()

javascripthtmlpopup

提问by hop

Will I be able to display a HTML content (not a file) in a pop up window using JS?

我可以使用 JS 在弹出窗口中显示 HTML 内容(不是文件)吗?

I am trying to open a pop up window and display an user defined HTML inside this. I am trying the below, but it doesn't work.

我正在尝试打开一个弹出窗口并在其中显示用户定义的 HTML。我正在尝试以下方法,但不起作用。

window.showModalDialog("<p>Pop up window text</p>","resizable: yes");

Can some one suggest me how to do this? Thanks in advance.

有人可以建议我如何做到这一点吗?提前致谢。

回答by Jo?o Silva

Contrary to window.open(), the first (URL) argument of showModalDialog()is required. Thus, you can't pass it an HTML string. You can either use window.open:

与 相反window.open(), 的第一个(URL)参数showModalDialog()是必需的。因此,您不能将 HTML 字符串传递给它。您可以使用window.open

var newWindow = window.open("", "newWindow", "resizable=yes");
newWindow.document.write('<p>Pop up window text</p>');

Or alternatively, use one the existing modal plugins, such as jQuery UI Dialogor Twitter Bootstrap Modals. They allow you to easily display a modal window based on the content of an existing HTML element, e.g.:

或者,使用现有的模态插件之一,例如jQuery UI DialogTwitter Bootstrap Modals。它们允许您根据现有 HTML 元素的内容轻松显示模式窗口,例如:

<div id="dialog">
  <p>Pop up window text</p>
</div>

$('#dialog').modal(); // Twitter Bootstrap
$("#dialog").dialog({ resizable: true }); // jQuery UI