如何仅使用 JavaScript 获取下一个元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15289932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:13:36  来源:igfitidea点击:

How to get next element using JavaScript-only?

javascriptprepend

提问by

Let's say we have this markup:

假设我们有这个标记:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>project.js</title>
<script src="project.js"></script>
<script>

</script>
</head>

<body>
<h1>some project &mdash; javascript & html tests</h1>
<hr />

    <p>
        testing 123
    </p>
</body>
</html>

I know that there are .prependChild(), .appendChild(), .innerHTML, etc, properties and methods, but what I am looking for is how to add (append) contents after the </body>tag closure?

我知道有.prependChild().appendChild().innerHTML,等属性和方法,但我所寻找的是如何在后添加(附加)内容</body>标签关闭?

I need this, without using jQuery— is it possible?

我需要这个,而不使用 jQuery——这可能吗?

采纳答案by Amrendra

If you use 'id'.you can use these property:

如果您使用“id”。您可以使用这些属性:

document.getElementById('id').nextSibling; 
document.getElementById('id').previousSibling;

回答by Roman

It won't work in some browser because content after bodyis not "legal". Anyway, this would be:

它在某些浏览器中不起作用,因为之后的body内容不“合法”。无论如何,这将是:

document.body.parentNode.appendChild(document.createTextNode('text after body'));
document.body.parentNode.appendChild(document.createComment('comment after body'));

http://jsfiddle.net/yVKk6/and inspect the Result frame.

http://jsfiddle.net/yVKk6/并检查结果框架。