Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问
1、常见对象属性
document.title //设置文档标题等价于HTML的
2、常用对象方法
document.write() //动态向页面写入内容 document.createElement(Tag) //创建一个html标签对象 document.getElementById(ID) //获得指定ID值的对象 document.getElementsByName(Name) //获得指定Name值的对象 document.body.appendChild(oTag)
3、body-主体子对象
document.body //指定文档主体的开始和结束等价于
4、常用对象事件:
document.body.onclick="func()" //鼠标指针单击对象是触发 document.body.onmouseover="func()" //鼠标指针移到对象时触发 document.body.onmouseout="func()" //鼠标指针移出对象时触发
5、location-位置子对象:
document.location.hash // #号后的部分 document.location.host // 域名+端口号 document.location.hostname // 域名 document.location.href // 完整URL document.location.pathname // 目录部分 document.location.port // 端口号 document.location.protocol // 网络协议(http:) document.location.search // ?号后的部分
6、常用对象事件:
documeny.location.reload() //刷新网页 document.location.reload(URL) //打开新的网页 document.location.assign(URL) //打开新的网页 document.location.replace(URL) //打开新的网页
selection-选区子对象 document.selection
images集合(页面中的图象):----------------------------a)通过集合引用 document.images //对应页面上的标签 document.images.length //对应页面上
标签的个数 document.images[0] //第1个
标签 document.images[i] //第i-1个
标签----------------------------b)通过name属性直接引用
document.images.oImage //document.images.name属性----------------------------c)引用图片的src属性 document.images.oImage.src //document.images.name属性.src----------------------------d)创建一个图象 var oImage oImage = new Image() document.images.oImage.src="1.jpg"同时在页面上建立一个
标签与之对应就可以显示----------------------------示例代码(动态创建图象):
var oImage
oImage = new Image()
document.images.oImage.src="1.jpg"
oImage=document.caeateElement("IMG")
oImage.src="1.jpg"
document.body.appendChild(oImage)
forms集合(页面中的表单):----------------------------a)通过集合引用 document.forms //对应页面上的
document.Myform.myctrl //document.表单名.控件名----------------------------c)访问表单的属性 document.forms[i].name //对应function fun(){
//遍历checkbox控件的值并判断是否选中
var length
length=document.forms[0].chk.length
for(i=0;i v=document.forms[0].chk[i].value b=document.forms[0].chk[i].checked if(b) alert(v=v+"被选中") else alert(v=v+"未选中") } } ----------------------------示例代码(Select):
//遍历select控件的option项
var length
length=document.Myform.oSelect.length
for(i=0;i document.write(document.Myform.oSelect[i].value) //遍历option项并且判断某个option是否被选中 for(i=0;i if(document.Myform.oSelect[i].selected!=true) document.write(document.Myform.oSelect[i].value) else document.write(""+document.Myform.oSelect[i].value+"") } //根据SelectedIndex打印出选中的option //(0到document.Myform.oSelect.length-1) i=document.Myform.oSelect.selectedIndex document.write(document.Myform.oSelect[i].value) //动态增加select控件的option项 var oOption = document.createElement("OPTION"); oOption.text="4"; oOption.value="4"; document.Myform.oSelect.add(oOption); Div集合(页面中的层): ----------------------------图层对象的4个属性 document.getElementById("ID").innerText //动态输出文本 document.getElementById("ID").innerHTML //动态输出HTML document.getElementById("ID").outerText //同innerText document.getElementById("ID").outerHTML //同innerHTML----------------------------示例代码: function change(){ document.all.oDiv.style.display="none" }
function changeText(){
document.getElementById("oDiv").innerText="NewText"
}
原文链接:Javascript document对象常用的方法和属性
更多参考:DOM中document对象的常用属性方法