1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > JavaScript中document.getElementById和document.write

JavaScript中document.getElementById和document.write

时间:2021-08-11 20:29:31

相关推荐

JavaScript中document.getElementById和document.write

1、操作HTML元素

JavaScript访问某个HTML元素,可以使用document.getElementById(id)方法。

eg:

<!DOCTYPE html> <html> <body> <p id="demo">Hello</p> <script> document.getElementById("demo").innerHTML="Hello world"; </script> </body> </html>

2、写到文档输出

JavaScript写到文档输出,可以使用document.write('HTML内容')方法。

eg:

<!DOCTYPE html> <html> <body> <script> document.write("<p>Hello world</p>"); </script> </body> </html>

注意:

请使用 document.write() 仅仅向文档输出写内容。

如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖:

<!DOCTYPE html>

<html>

<body>

<p>Hello world</p>

<button οnclick="myFunction()">点击这里</button>

<script>

function myFunction(){

document.write("糟糕!消失了。");

}

</script>

</body>

</html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。