gpt4 book ai didi

javascript - 有什么方法可以选择 ``标签吗?使用 `getElementById("bodyTagId")` but not working with ` getElementsByTagName ("body")`

转载 作者:行者123 更新时间:2023-12-02 22:20:41 24 4
gpt4 key购买 nike

我正在开发支持聊天网站,我想将ajax html的响应附加到body标记的内部html之后。

例如:在h2标签后附加html(这是网页的html。)

function insertAfter(referenceNode, newNode) {                                                           
referenceNode.parentNode.insertBefore(newNode,referenceNode.nextSibling);
}

var el = document.createElement("span");
el.innerHTML = "<p>test</p>";
var div = document.getElementsByTagName("body");
insertAfter(div, el);
<html>
<head>
<title>script example</title>
</head>
<body>
<h2>here is html of web page.</h2>
</body>
</html>

最佳答案

getElementsByTagName 将返回一个数组。因此,您必须获取数组的第一个元素才能获取实际的正文标签。类似下面的内容

var bodyTag = document.getElementsByTagName("body");
bodyTag[0].append(...);

关于javascript - 有什么方法可以选择 `<body>`标签吗?使用 `getElementById("bodyTagId")` but not working with ` getElementsByTagName ("body")`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59242717/

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