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

CSS*_*Guy 5 html javascript

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

<script>
alert("<html><body><h1>this is alert heading</h1></html></body>")
</script>
Run Code Online (Sandbox Code Playgroud)

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

mar*_*ljn 7

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

alert("\<html\>\<body\>\<h1\>this is alert heading\</h1\>\</html\>\</body\>")
Run Code Online (Sandbox Code Playgroud)

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


Gui*_*tos 6

我会使用Pop Up而不是使用警报.我建议你使用像jQuery Dialog这样的东西,看看http://jqueryui.com/demos/dialog/

样品:

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

<div id='my-dialog-id' title='my dialog title'>
  <h1>this is alert heading</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

然后包含一些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>
Run Code Online (Sandbox Code Playgroud)

它准备好了!