DOM――获取页面信息
2011年10月24日
1、获得文档标题
document.write(document.title)
2、获得文档的URL
document.write(document.URL)
3、获得文档的referrer
document.write(document.referrer)
4、获得文档的服务器域名
document.write(document.domain)
5、获取ID的值
function getValue()
{
var x=document.getElementById("myHeader")
alert(x.innerHTML)
}
这是标题
点击标题,会提示出它的值。
6、获取name的值
function getElements()
{
var x=document.getElementsByName("myInput");
alert(x.length);
}
7、打开并写入一个新文档
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="学习 DOM 非常有趣!";
newDoc.write(txt);
newDoc.close();
}
8、返回文档中锚的数目
第一个锚
第二个锚
第三个锚
文档中锚的数目:
document.write(document.anchors.length)
9、返回文档中第一个锚的innerHTML
第一个锚
第二个锚
第三个锚
本文档中第一个锚的 InnerHTML 是:
document.write(document.anchors[0].innerHTML)
10、返回文档中表单的数目
document.write("文档包含: " + document.forms.length + " 个表单。")
11、返回多个表单中某一个的name
您的姓名:
您的汽车:
要访问集合中的项目,你既可以使用项目编号,也可以使用项目名称:
document.write("第一个表单名称是:" + document.forms[0].name + "")
document.write("第一个表单名称是:" + document.getElementById("Form1").name + "")
12、返回文档中图片的个数
document.write("本文档包含:" + document.images.length + " 幅图像。")
2011年10月24日
1、获得文档标题
document.write(document.title)
2、获得文档的URL
document.write(document.URL)
3、获得文档的referrer
document.write(document.referrer)
4、获得文档的服务器域名
document.write(document.domain)
5、获取ID的值
function getValue()
{
var x=document.getElementById("myHeader")
alert(x.innerHTML)
}
这是标题
点击标题,会提示出它的值。
6、获取name的值
function getElements()
{
var x=document.getElementsByName("myInput");
alert(x.length);
}
7、打开并写入一个新文档
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="学习 DOM 非常有趣!";
newDoc.write(txt);
newDoc.close();
}
8、返回文档中锚的数目
第一个锚
第二个锚
第三个锚
文档中锚的数目:
document.write(document.anchors.length)
9、返回文档中第一个锚的innerHTML
第一个锚
第二个锚
第三个锚
本文档中第一个锚的 InnerHTML 是:
document.write(document.anchors[0].innerHTML)
10、返回文档中表单的数目
document.write("文档包含: " + document.forms.length + " 个表单。")
11、返回多个表单中某一个的name
您的姓名:
您的汽车:
要访问集合中的项目,你既可以使用项目编号,也可以使用项目名称:
document.write("第一个表单名称是:" + document.forms[0].name + "")
document.write("第一个表单名称是:" + document.getElementById("Form1").name + "")
12、返回文档中图片的个数
document.write("本文档包含:" + document.images.length + " 幅图像。")