【问题标题】:How to create a new window with text and an image如何创建带有文本和图像的新窗口
【发布时间】:2021-07-18 01:38:47
【问题描述】:

这是我的代码,我想在下面添加一张图片:

function createWindow() {
    var myWindow = window.open("", "MsgWindow", "width=500,height=500");
    myWindow.document.write("<h1>A test: </h1>" + 
    " <p> <b>Something bold: </b> text </p>");
    // I want to add an image here (inside the widow) but I'm not sure how

}

【问题讨论】:

  • 你试过&lt;img src="imagesource"&gt;吗?
  • document.write 可以避免。您可以通过传递 url 在 window.open 中加载 html 文件

标签: javascript html image testing window


【解决方案1】:

您可以像这样将图像添加到myWindow 的html 文档中:

myWindow.document.write("<img src='https://i.imgur.com/RUKhJjI.png' width=256 height=256>");

【讨论】:

    【解决方案2】:
    function createWindow() {
      const myWindow = window.open('', 'My New Window', 'width=500,height=500')
      myWindow.document.body.innerHTML = `
        <h1>A test:</h1>
        <p> <b>Something bold: </b> text </p>
        <img src="https://i.imgur.com/qVr8o7q.jpeg" />
      `;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 2012-11-11
      • 2011-02-07
      • 2012-10-31
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多