Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。

 

属性

1  document.anchors  返回对文档中所有 Anchor 对象的引用。还有document.links/document.forms/document.images等

2  document.URL       返回当前文档的url

3  document.title       返回当前文档的标题

4  document.body    返回body元素节点

 

方法

1  document.close()     关闭用 document.open() 方法打开的输出流,并显示选定的数据。

<!DOCTYPE html>
<html>
<head>
<script>
function createDoc()
{
var w=window.open();
w.document.open();
w.document.write("<h1>Hello World!</h1>");
w.document.close();
}
</script>
</head>

<body>
<input type="button" value="New document in a new window" onclick="createDoc()">
</body>
</html>
View Code

2  document.createElement()     创建元素节点。

<!DOCTYPE html>
<html lang="en">
<!DOCTYPE html>
<head>
</head>
<body>
    <p>hello world</p>
    
<script>
    var a = document.createElement('hr');
    document.body.appendChild(a)


</script>
</body>
</html>
View Code

3  document.createAttribute()   创建属性节点。

<!DOCTYPE html>
<html>
<body>

<p >Click the button to make a BUTTON element.</p>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction()
{
var btn=document.createElement("BUTTON");
document.body.appendChild(btn);
};

</script>

</body>
</html>
View Code

相关文章:

  • 2021-04-20
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-05-16
  • 2022-12-23
  • 2021-09-10
猜你喜欢
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-06-10
相关资源
相似解决方案