在javascript中的div元素内创建div元素 [英] Creating a div element inside a div element in javascript
本文介绍了在javascript中的div元素内创建div元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在已经存在的 div
中创建 div
的一个非常基本的示例。
I'm trying a very basic example of creating a div
inside an already existing div
.
使用时它似乎不起作用:
It doesn't seem to be working when I use:
document.getElementbyId('lc').appendChild(element)
但是当我这样做时工作正常:
but works fine when I do this:
document.body.appendChild(element)
我是否需要添加 windows.onload
功能?虽然它甚至不起作用!
Do I need to add windows.onload
function? Though it doesn't work even then!
HTML code:
HTML code:
<body>
<input id="filter" type="text" placeholder="Enter your filter text here.." onkeyup = "test()" />
<div id="lc">
</div>
</body>
JS代码:
function test()
{
var element = document.createElement("div");
element.appendChild(document.createTextNode('The man who mistook his wife for a hat'));
document.getElementbyId('lc').appendChild(element);
//document.body.appendChild(element);
}
推荐答案
你的代码很适合你错误输入以下代码行:
Your code works well you just mistyped this line of code:
document.getElementbyId('lc').appendChild(element);
用此更改:
document.getElementById('lc').appendChild(element);
这是我的例子:
<html>
<head>
<script>
function test() {
var element = document.createElement("div");
element.appendChild(document.createTextNode('The man who mistook his wife for a hat'));
document.getElementById('lc').appendChild(element);
}
</script>
</head>
<body>
<input id="filter" type="text" placeholder="Enter your filter text here.." onkeyup = "test()" />
<div id="lc" style="background: blue; height: 150px; width: 150px;
}" onclick="test();">
</div>
</body>
</html>
这篇关于在javascript中的div元素内创建div元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文