document对象学习

[b]getElementById方法[/b]

<div id="div2"><p id="p1">test world</p></div>


var e = document.getElementById('div2');
//1,DIV,null
alert("nodeType:" + e.nodeType + "\n"
+ "nodeName:" + e.nodeName +"\n"
+ "nodeValue" + e.nodeValue);

var e = document.getElementById('p1');
e= e.firstChild;
//3,#text,test world
alert("nodeType:" + e.nodeType + "\n"
+ "nodeName:" + e.nodeName + "\n"
+ "nodeValue:" + e.nodeValue);


说明:
1.nodeType:ELEMENT_NODE值为1;ATTRIBUTE_NODE值为2;TEXT_NODE值为3
2.nodeName 为 tagName(HTML文档一般会返回大写,二XHTML文档则根据页面来显示。为保险起见,最好进行大小写转换toLowerCase()或toUpperCase()),文本值为#text;
3.nodeValue 为null,文本值为文本的内容。

[b]getElementsByTagName方法[/b]
返回一个HTMLCollection对象,与NodeList(例如 someNode.childNodes)一样,也是'活'的。
HTMLCollection对象提供 item(index)获取元素,也可以通过namedItem(name)来获取里面的元素:

< img src='myimage.gif' name='myImage'>


var images = document.getElementsByTagName("img");
var myimage = images.namedItem('myImage');
var myImage = images.item(0);
//同样也可以通过中括号来(名称或索引)访问。
var myImage = images['myImage'];
var myImage = images[0];

[b]常见属性应用[/b]

//document 内置了几个 HTMLCollection类型的属性,用于快速获取一些页面对象
document.anchors — 所有带有name属性的< a >对象。
document.applets — 所有< applet >对象,不再提倡使用
document.forms — 所有 < form > 对象. 等同于 document.getElementsByTagName('form') .
document.images — 所有< img > 对象. 同于document.getElementsByTagName('img').
document.links — 所有带有href属性的< a > 对象。
document.cookie - 返回空字符串或由 name=value形式组合的,由;号分隔的字符串。

//代表了可视范围的空间,会根据浏览器窗口大小的改变而改变
alert(document.documentElement.clientHeight);
//代表了内容占据的空间
alert(document.body.clientHeight);


document是Document的实例。以下是level-one-html 里面关于Document的IDL定义:


interface HTMLDocument : Document {
attribute DOMString title;
readonly attribute DOMString referrer;
readonly attribute DOMString domain;
readonly attribute DOMString URL;
attribute HTMLElement body;
readonly attribute HTMLCollection images;
readonly attribute HTMLCollection applets;
readonly attribute HTMLCollection links;
readonly attribute HTMLCollection forms;
readonly attribute HTMLCollection anchors;
attribute DOMString cookie;
void open();
void close();
void write(in DOMString text);
void writeln(in DOMString text);
Element getElementById(in DOMString elementId);
NodeList getElementsByName(in DOMString elementName);
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值