当前页面: 开发资料首页 → Javascript 专题 → JavaScript脚本语言描述:document 文挡对象详解
摘要: JavaScript脚本语言描述:document 文挡对象详解
---------------------------------------------------------------------
注:页面上元素name属性和JavaScript引用的名称必须一致包括大小写
否则会提示你一个错误信息 "引用的元素为空或者不是对象"
---------------------------------------------------------------------
对象属性
document.title //设置文档标题等价于HTML的
images集合(页面中的图象)
a)通过集合引用
document.images //对应页面上的标签
document.images.length //对应页面上标签的个数
document.images[0] //第1个标签
document.images[i] //第i-1个标签
b)通过nane属性直接引用
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"
同时在页面上建立一个标签与之对应就可以显示
<script language="javascript">
var oImage
oImage = new Image()
document.images.oImage.src="/1.jpg"
</script>
----------------------------------------------------------------------
forms集合(页面中的表单)
a)通过集合引用
document.forms //对应页面上的<form>标签
document.forms.length //对应页面上<form>标签的个数
document.forms[0] //第1个<form>标签
document.forms[i] //第i-1个<form>标签
document.forms[i].length //第i-1个<form>中的控件数
document.forms[i].elements[j] //第i-1个<form>中第j-1个控件
b)通过标签name属性直接引用
<form name="Myform"><input name="myctrl"></form>
document.Myform.myctrl //document.表单名.控件名
-----------------------------------------------------------------------
<script language="javascript">
//遍历select控件的option项
var length
length=document.Myform.oSelect.length
for(i=0;i
</script>
<script language="javascript">
//遍历option项并且判断某个option是否被选中
for(i=0;i
document.write(document.Myform.oSelect[i].value)
else
document.write(""+document.Myform.oSelect[i].value+"")
}
</script>
<script language="javascript">
//根据SelectedIndex打印出选中的option
//(0到document.Myform.oSelect.length-1)
i=document.Myform.oSelect.selectedIndex
document.write(document.Myform.oSelect[i].value)
</script>
<script language="javascript">
//动态增加select控件的option项
var oOption = document.createElement("OPTION");
oOption.text="4";
oOption.value="4";
document.Myform.oSelect.add(oOption);
</script>
-----------------------------------------------------------------------