通过JavaScript动态创建和打印h1标签 [英] Dynamically create and print h1 tags through JavaScript
本文介绍了通过JavaScript动态创建和打印h1标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要能够在JavaScript中创建一个函数,其中我所要做的只是键入h1("hello"),它将显示问候.
I need to be able to create a function in JavaScript where all I need to do is type h1("hello") and it will print hello.
我想避免这种方法:
function h1(text) {
document.write('<h1>'+text+'</h1>');
}
这就是我所拥有的:
function h1(text) {
var div = document.createElement('div');
document.appendChild(div);
var h1 = document.createElement('h1');
div.appendChild(h1);
h1.createTextNode(text);
}
推荐答案
<script>
function myFunction(text) {
var h = document.createElement("H1");
var t = document.createTextNode(text);
h.appendChild(t);
document.body.appendChild(h);
}
</script>
这篇关于通过JavaScript动态创建和打印h1标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文