首页 > 设计 > WEB开发 > 正文

JavaScript中document.getElementById和document.write

2019-11-02 18:29:15
字体:
来源:转载
供稿:网友

1、操作HTML元素

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

例子:

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

显示效果:

2、写到文档输出

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

例子:

<!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 onclick="myFunction()">点击这里</button><script>function myFunction(){   document.write("糟糕!消失了。");}</script></body></html>显示效果:

点击点击这里出现的效果:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表