gpt4 book ai didi

javascript - 在javascript中的div元素内创建一个div元素

转载 作者:IT王子 更新时间:2023-10-29 03:23:16 26 4
gpt4 key购买 nike

我正在尝试一个非常基本的示例,即在一个已经存在的 div 中创建一个 div

当我使用时它似乎不起作用:

document.getElementbyId('lc').appendChild(element)

但是当我这样做时工作正常:

document.body.appendChild(element)

我需要添加windows.onload函数吗?虽然即使那样也行不通!

HTML代码:

<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);
}

最佳答案

您的代码运行良好,您只是输错了这行代码:

document.getElementbyId('lc').appendChild(element);

将其更改为:(“B”应大写。)

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 - 在javascript中的div元素内创建一个div元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com