Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中返回HTML,同时包含我创建的变量?_Javascript - Fatal编程技术网

如何在javascript中返回HTML,同时包含我创建的变量?

如何在javascript中返回HTML,同时包含我创建的变量?,javascript,Javascript,我想使用javascript创建以下div块: <div class=id="hello"> <h1 class="header-1"></h1> <img> <p class="paragraph-1"></p> </div> { } ?正如注释中提到的@Ci

我想使用javascript创建以下div块:


      <div class=id="hello">
          <h1 class="header-1"></h1>
          <img>
          <p class="paragraph-1"></p>
      </div>

{

}

正如注释中提到的@Cid,使用
document.createElement()
是个好主意


const div=document.createElement(“div”);//创建一个div
常量h1=document.createElement(“h1”);//创建一个h1
常量img=document.createElement(“img”);//创建图像标记
常量p=document.createElement(“p”);//创建段落
div.classList.add(“hello”);//分配“hello”css类
div.id=“你好”
h1.classList.add(“header-1”);//分配“标题”css类
p、 类列表。添加(“第1段”);//分配“段落1”css类
子类附件(h1);//嵌套h1内部分区
儿童分部(img);//嵌套img内部div
附属儿童(p);//嵌套p在div内
document.body.appendChild(div);//将div添加到当前文档

正如注释中提到的@Cid,使用
document.createElement()
是个好主意


const div=document.createElement(“div”);//创建一个div
常量h1=document.createElement(“h1”);//创建一个h1
常量img=document.createElement(“img”);//创建图像标记
常量p=document.createElement(“p”);//创建段落
div.classList.add(“hello”);//分配“hello”css类
div.id=“你好”
h1.classList.add(“header-1”);//分配“标题”css类
p、 类列表。添加(“第1段”);//分配“段落1”css类
子类附件(h1);//嵌套h1内部分区
儿童分部(img);//嵌套img内部div
附属儿童(p);//嵌套p在div内
document.body.appendChild(div);//将div添加到当前文档

无效HTML
const someHTML='你好,世界

您可以使用too@Cid这很有效-我已经忘了怎么做了,谢谢你。如何引用js对象并使用is属性设置元素的类名?可以使用字符串串联
is Not valid HTML
const someHTML='hello world

您可以使用too@Cid这很有效-我已经忘了怎么做了,谢谢你。如何引用js对象并使用is属性设置元素的类名?可以使用字符串连接
 {<div class=id="hello">
          <h1 class="header-1"></h1>
          <img>
          <p class="paragraph-1"></p>
      </div>}