javascript - 使用javascript将两个图像加载到html

标签 javascript html css

我想将两个图像加载到我的 HTML 页面并使用 Javascript 将它们并排(水平)放置。但在第一步,会发生困惑。

这里我有一段代码,结果是这样的:

enter image description here

我该如何解决?我的错在哪里?

Javascript:

var img = document.createElement("IMG");
  img.setAttribute("src", "1.jpg");
  img.setAttribute("width", "300");
  img.setAttribute("height", "300");
  img.setAttribute("alt", "The Pulpit Rock");
  document.body.appendChild(img);

  var img2 = document.createElement("IMG2");
  img2.setAttribute("src", "2.jpg");
  img2.setAttribute("width", "300");
  img2.setAttribute("height", "300");
  img2.setAttribute("alt", "The Pulpit Rock2");
  document.body.appendChild(img2);

HTML:

<div id="IMG">
<div id="IMG2">

<script src="Script.js"></script>
<link rel="stylesheet" type="text/css" href="Style.css">

CSS:

img {

   border: 1px solid #d6d6d6;
   padding: 6px;
   border-radius: 50%;
   box-shadow: 0 4px 8px 0 rgba(227, 228, 232), 0 6px 20px 0        rgba(227, 228, 232);  

}


img2 {

   border: 1px solid #d6d6d6;
   padding: 6px;
   border-radius: 50%;
   box-shadow: 0 4px 8px 0 rgba(227, 228, 232), 0 6px 20px 0        rgba(227, 228, 232);  

}

最佳答案

为您的img2 , 你有 var img2 = document.createElement("IMG2"); ,这将创建一个 <img2>元素。这需要更改为 var img2 = document.createElement("IMG");创建一个 <img>元素。

这可以从以下方面看出:

var img = document.createElement("IMG");
img.setAttribute("src", "http://placehold.it/100");
img.setAttribute("width", "300");
img.setAttribute("height", "300");
img.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(img);

var img2 = document.createElement("IMG");
img2.setAttribute("src", "http://placehold.it/100");
img2.setAttribute("width", "300");
img2.setAttribute("height", "300");
img2.setAttribute("alt", "The Pulpit Rock2");
document.body.appendChild(img2);
img {
  border: 1px solid #d6d6d6;
  padding: 6px;
  border-radius: 50%;
  box-shadow: 0 4px 8px 0 rgba(227, 228, 232), 0 6px 20px 0 rgba(227, 228, 232);
}

img2 {
  border: 1px solid #d6d6d6;
  padding: 6px;
  border-radius: 50%;
  box-shadow: 0 4px 8px 0 rgba(227, 228, 232), 0 6px 20px 0 rgba(227, 228, 232);
}

关于javascript - 使用javascript将两个图像加载到html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874516/

相关文章:

javascript - 嵌套数组的排序不会导致顺序发生变化

javascript - 我如何在 Node.js 中对某些内容进行 URl 编码?

Coffeescript 中用于 Google Analytics 代码的 Javascript 范围

html - 为什么我们需要斜体/粗体文件? HTML Italic/Strong 标签和 Font 之间有什么区别?

css - 在 Dash 布局中创建混合宽度和高度的单元格

css - 在响应式 Bootstrap 导航栏中居中内容

javascript - 如何修复 nginx 上的 http 414 Request-URI Too Large 错误?

javascript - 我可以自动提交文件上传吗?

html - 如何让背景图像/图像通过 CSS 拉伸(stretch)以填充容器?

jquery - 有什么方法可以使 div 在高度变化时垂直居中?