Javascript 在与 iframe 相同的警报窗口中显示 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10689556/
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-08-24 02:31:10  来源:igfitidea点击:

show HTML in alert window same like iframe?

javascripthtml

提问by CSS Guy

i want to show HTML in alert window same like iframe..? can i do this...

我想在与 iframe 一样的警报窗口中显示 HTML ..?我可以这样做吗...

<script>
alert("<html><body><h1>this is alert heading</h1></html></body>")
</script>

how can i do this..?

我怎样才能做到这一点..?

回答by marteljn

If your asking how to display the HTML markup per se in the alert box, escape it:

如果您询问如何在警告框中显示 HTML 标记本身,请将其转义:

alert("\<html\>\<body\>\<h1\>this is alert heading\</h1\>\</html\>\</body\>")

If you are asking how to format the alert box using HTML you cannot. You need to create a modal dialog box as one of the other answers indicates.

如果您要问如何使用 HTML 格式化警报框,则不能。您需要创建一个模态对话框,如其他答案之一所示。

回答by Guilherme de Jesus Santos

Instead of using alert, I would use a Pop Up. And I recommend you to use something like jQuery Dialog , look at http://jqueryui.com/demos/dialog/

我会使用弹出窗口而不是使用警报。我建议你使用类似 jQuery Dialog 的东西,看看http://jqueryui.com/demos/dialog/

Sample :

样本 :

First, create a div to wrap your html elements,

首先,创建一个 div 来包装你的 html 元素,

<div id='my-dialog-id' title='my dialog title'>
  <h1>this is alert heading</h1>
</div>

Then include some javascript with jQuery and jQueryUI

然后在 jQuery 和 jQueryUI 中包含一些 javascript

  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $(function() {
    $("#my-dialog-id").dialog(); //Here I select a div with id my-dialog-id and start the plugin.
  } );
  </script>

And it's ready!

它已经准备好了!

回答by ceejayoz

You can't do it via alert().

你不能通过alert().

You can mimic it using a modal dialog. There are lots of modal dialog libraries - jQuery UIhas a pretty powerful one.

您可以使用模态对话框来模仿它。有很多模态对话框库——jQuery UI有一个非常强大的。