技术共享
技术共享 Technology sharing 当前位置:首页->技术共享->技术共享

1.使用document.write()向输出流写文本
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
2.使用document.write()向输出流写HTML
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>
3.返回当前文档的URL
<html>
<body>
该文档的 URL 是:
<script type="text/javascript">
document.write(document.URL)
</script>
</body>
</html>
4.使用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>
5.使用getElementByName
<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>
6.打开一个新文档,添加一些文本,然后关闭它
<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>
7.返回文档中锚的数目
<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>
8.返回文档中第一个锚的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>
9.返回文档中表单的数目
<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>
10.返回文档中图像的数目
<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>




上一篇:php缩略图函数
下一篇:$_SERVER详解