[英]how to add a h1 inside a div using javascript
我正在编写一个 HTML,我想在其中使用Javascript
填充h1
。 在这里,我可以填充文本,但不能填充h1
。 下面是我的代码。
function getTheIncident(keyWord, text) {
showTheDiv();
console.log(keyWord + "\t" + text);
var e = document.getElementById('textResult');
e.innerHTML = text.replace('\^', '\'');
}
这给了我如下图所示的输出。
我的目标是添加一个h1
,内容为Description
。 基本上喜欢
<h1>Description</h1>
正上方A procurement order has been placed for a RAM with 128 Gig
请让我知道我该怎么做。
var h1= document.createElement('H1');
h1.innerHTML = "Description";
现在将此元素添加到您想要的特定节点
只需将要添加为 HTML 的代码包装到h1
:
function getTheIncident(keyWord, text) {
showTheDiv();
console.log(keyWord + "\t" + text);
var e = document.getElementById('textResult');
e.innerHTML = "<h1>" + text.replace('\^', '\'') + "</h1>";
}
https://fiddle.jshell.net/LnkfLrev/1/ 这样的东西? 你可以使用document.getElementById('placeholder').innerHTML = text;
设置你的h1
元素的文本
编辑:
https://fiddle.jshell.net/LnkfLrev/2/
对不起,我没有安静地问你问题。 我更新了小提琴所以它可以作为一个答案
我用 var h = document.createElement("H1");
创建了一个 H1 元素h = document.createElement("H1");
然后我设置文本并将其添加到正文
h.innerHTML = text;
document.body.appendChild(h);
希望这现在有帮助
使用createElement()
创建您的标签,然后将其prepend()
到您想要首先放置的容器中。 检查以下片段以供参考。
var h1Tag = document.createElement('H1'); h1Tag.innerHTML = "Your Title"; $('.output').prepend(h1Tag);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="output"> This div contains the output.. </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.