[b]getElementById方法[/b]
说明:
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)来获取里面的元素:
[b]常见属性应用[/b]
document是Document的实例。以下是level-one-html 里面关于Document的IDL定义:
<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);
};