gpt4 book ai didi

javascript - JS 中的 createTextNode 在 Chrome 中不起作用

转载 作者:行者123 更新时间:2023-11-30 18:02:37 24 4
gpt4 key购买 nike

我正在尝试一个简单的练习,我想将一个由 JavaScript 生成的

附加到一个 id 为“target-area”的 div。问题是 Chrome 拒绝注入(inject)这一段并给我一个错误:未捕获的类型错误:对象 # 没有方法 'createTextNode'

代码如下:

<html>
<head>
<title>JS</title>

</head>
<body>
<div id="target-area">
<p id="tagline">Hello World!</p>
</div>

</body>
<script type="text/javascript">

// store the target area to a variable to keep things neat
var targetArea = document.getElementById("target-area");
// create our <p> element
var p = document.createElement("p");
// create a text node inside the <p>, note that we're using a variable "p" here
var snippet = p.createTextNode("this was a generated paragraph");
// insert our generated paragraph into the DOM
targetArea.appendChild(snippet);

</script>
</html>

谢谢!

最佳答案

文档有 createTextNode 属性而不是刚刚创建的元素

所以基本上你的代码应该是这样的..

    // store the target area to a variable to keep things neat
var targetArea = document.getElementById("target-area");
// create our <p> element
var p = document.createElement("p");
// create a text node inside the <p>, note that we're
// using a variable "p" here
var snippet = document.createTextNode("this was a generated paragraph");
// insert our generated paragraph into the DOM
p.appendChild(snippet);
targetArea.appendChild(p);

Check Fiddle

关于javascript - JS 中的 createTextNode 在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16549511/

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