使用 document.write() 向输出流写文本

<html>
<body>

<script type="text/javascript">
document.write("Hello World!")
</script>

</body>
</html>

使用 document.write() 向输出流写 HTML

<html>
<body>

<script type="text/javascript">
document.write("<h1>Hello World!</h1>")
</script>

</body>
</html>

返回当前文档的标题

<html>
<head>
<title>My title</title>
</head>

<body>
该文档的标题是:
<script type="text/javascript">
document.write(document.title)
</script>
</body>

</html>

返回当前文档的 URL

<html>

<body>
该文档的 URL 是:
<script type="text/javascript">
document.write(document.URL)
</script>
</body>

</html>

返回当前文档的 referrer

<html>
<body>

<p>referrer 属性返回加载本文档的文档的 URL。</p>

本文档的 referrer 是:
<script type="text/javascript">
document.write(document.referrer)
</script>

</body>
</html>

返回下载当前文档的服务器域名

<html>
<body>

本文档的域名是:
<script type="text/javascript">
document.write(document.domain)
</script>

</body>
</html>

使用 getElementById()

<html>
<head>
<script type="text/javascript">
function getValue()
{
var x=document.getElementById("myHeader")
alert(x.innerHTML)
}
</script>
</head>
<body>

<h1 id="myHeader" onclick="getValue()">这是标题</h1>
<p>点击标题,会提示出它的值。</p>

</body>
</html>

使用 getElementsByName()

<html>
<head>
<script type="text/javascript">
function getElements()
  {
  var x=document.getElementsByName("myInput");
  alert(x.length);
  }
</script>
</head>

<body>
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<br />
<input type="button" onclick="getElements()" value="名为 'myInput' 的元素有多少个?" />
</body>

</html>

打开一个新的文档,添加一些文本,然后关闭它。

<html>
<head>
<script type="text/javascript">
function createNewDoc()
  {
  var newDoc=document.open("text/html","replace");
  var txt="<html><body>学习 DOM 非常有趣!</body></html>";
  newDoc.write(txt);
  newDoc.close();
  }
</script>
</head>

<body>
<input type="button" value="打开并写入一个新文档" onclick="createNewDoc()">
</body>

</html>

返回文档中锚的数目

<html>
<body>

<a name="first">第一个锚</a><br />
<a name="second">第二个锚</a><br />
<a name="third">第三个锚</a><br />
<br />

文档中锚的数目:
<script type="text/javascript">
document.write(document.anchors.length)
</script>

</body>
</html>

返回文档中第一个锚的 innerHTML

<html>
<body>

<a name="first">第一个锚</a><br />
<a name="second">第二个锚</a><br />
<a name="third">第三个锚</a><br />
<br />

本文档中第一个锚的 InnerHTML 是:
<script type="text/javascript">
document.write(document.anchors[0].innerHTML)
</script>

</body>
</html>

计算文档中表单的数目

<html>
<body>

<form name="Form1"></form>
<form name="Form2"></form>
<form name="Form3"></form>

<script type="text/javascript">
document.write("文档包含: " + document.forms.length + " 个表单。")
</script>

</body>
</html>

访问集合中的项目

<html>
<body>
<form id="Form1" name="Form1">
您的姓名:<input type="text">
</form>
<form id="Form2" name="Form2">
您的汽车:<input type="text">
</form>

<p>
要访问集合中的项目,你既可以使用项目编号,也可以使用项目名称:
</p>

<script type="text/javascript">
document.write("<p>第一个表单名称是:" + document.forms[0].name + "</p>")
document.write("<p>第一个表单名称是:" + document.getElementById("Form1").name + "</p>")
</script>

</body>
</html>

计算文档中的图像数目

<html>
<body>
<img border="0" src="/i/eg_smile.gif" />
<br />
<img border="0" src="/i/eg_mouse.jpg" />
<br /><br />

<script type="text/javascript">
document.write("本文档包含:" + document.images.length + " 幅图像。")
</script>

</body>
</html>

HTML DOM 实例-Document 对象的更多相关文章

  1. javascript之DOM(二Document对象)

    javascript通过Document类型来表示文档.在浏览器中document是HTMLDocument对象(继承自Document)的一个实例,表示整个html页面.而且在浏览器中documen ...

  2. 【JavaScript】DOM之Document对象

    JS(JavaScript) 一.Document对象 1.Document对象是什么 Document对象 是DOM的基本规范也是重要的对象之一,以访问,更新页面内容的属性和方法通过conslie. ...

  3. HTML DOM部分---document对象;

    <style type="text/css"> #d3{ color:red} </style> </head> <body> &l ...

  4. DOM中document对象的常用属性方法

    每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1  document.anchors  返 ...

  5. js基础之DOM中document对象的常用属性方法

    -----引入 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1  document.an ...

  6. dom对象详解--document对象(二)

       dom对象详解--style对象 style对象 style对象和document对象下的集合对象styleSheets有关系,styleSheets是文档中所有style对象的集合,这里讲解的 ...

  7. JavaScript之HTML DOM Document 对象

    文档对象代表您的网页. 如果您希望访问 HTML 页面中的任何元素,那么您总是从访问 document 对象开始. 下面是一些如何使用 document 对象来访问和操作 HTML 的实例. 查找 H ...

  8. 报表软件JS开发引用HTML DOM的location和document对象

    上一次提到,在报表软件FineReport的JavaScript开发中,可以访问并处理的HTML DOM对象有windows.location.document三种.这次就继续介绍后两种,locati ...

  9. HTML DOM Document 对象

    HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM (Document Object Model) 中 , 每一个元素都是 节点: 文档是一个文档. 所有的HTML ...

随机推荐

  1. django中“url映射规则”和“服务端响应顺序”

    1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单.      查看方式:         import sys        print sys.path   ...

  2. oracle中schema指的是什么?

    看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...

  3. maven入门探讨

    java项目最恶心的一点莫过于需要使用大量的jar.每次引用jar的时候都要自己手动去各地寻找,然后导入到项目的指定文件夹当中最后还要添加Path.这无疑是一项工作量巨大的工作,同时如果控制不当就会提 ...

  4. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  5. Maven+SSM搭建总结(非教程)

    记录我用Maven搭建Spring+SpringMVC+Mybatis项目的过程. 网上关于这个的详细教程有很多,但是优质而适合自己看的需要自己筛选以下我看过的几篇认为讲的比较详细的资源(照着做吧,做 ...

  6. Android M新特性之APP Link

    The Android M Developer Preview introduces support for App Links, which improves upon existing link ...

  7. PureLayout和Masonry比较

    一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...

  8. Spark随笔(二):深入学习

    一.如何选择粗粒度和细粒度 从底层往上引申来理解粗粒度与细粒度. 一层:一个类,具有三个属性值.为了查询这个类的所有实例,细粒度查询的程度为属性值,即依次查询每个实例化对象的属性值,查询三次:粗粒度按 ...

  9. 安装debian第一天遇到的几个问题及解决方案

    1.当我想要使用sudo时,提示 bash: sudo: command not found 一开始以为是PATH不对,就各种百度各种试 export PATH=${PATH}:$HOME/bin:/ ...

  10. 使用查询(SQ01、SQ02、SQ03)创建报表

    查询需求说明 通过Query(SQ01.SQ02.SQ03)实现根据销售组织查询销售订单中的各项信息,包括物料.金额.成本,以及毛利. 其中对销售组织进行权限检查(Authority Check),字 ...