DOM学习1
来源:互联网 发布:怎么申请淘宝客服介入 编辑:程序博客网 时间:2024/05/21 15:48
DOM属性:
nodeName
报告节点的名称(详见下述)。 nodeValue
提供节点的 “值”(详见后述)。 parentNode
返回节点的父节点。记住,每个元素、属性和文本都有一个父节点。 childNodes
是节点的孩子节点列表。对于 HTML,该列表仅对元素有意义,文本节点和属性节点都没有孩子。 firstChild
仅仅是 childNodes
列表中第一个节点的快捷方式。 lastChild
是另一种快捷方式,表示 childNodes
列表中的最后一个节点。 previousSibling
返回当前节点之前 的节点。换句话说,它返回当前节点的父节点的 childNodes
列表中位于该节点前面的那个节点(如果感到迷惑,重新读前面一句)。 nextSibling
类似于 previousSibling
属性,返回父节点的 childNodes
列表中的下一个节点。 attributes
仅用于元素节点,返回元素的属性列表Example:
// These first two lines get the DOM tree for the current Web page,
// and then the <html> element for that DOM tree
var myDocument = document;
var htmlElement = myDocument.documentElement;
// What's the name of the <html> element? "html"
alert("The root element of the page is " + htmlElement.nodeName);
// Look for the <head> element
var headElement = htmlElement.getElementsByTagName("head")[0];
if (headElement != null) {
alert("We found the head element, named " + headElement.nodeName);
// Print out the title of the page
var titleElement = headElement.getElementsByTagName("title")[0];
if (titleElement != null) {
// The text will be the first child node of the <title> element
var titleText = titleElement.firstChild;
// We can get the text of the text node with nodeValue
alert("The page title is '" + titleText.nodeValue + "'");
}
// After <head> is <body>
var bodyElement = headElement.nextSibling;
while (bodyElement.nodeName.toLowerCase() != "body") {
bodyElement = bodyElement.nextSibling;
}
// We found the <body> element...
// We'll do more when we know some methods on the nodes.
}
DOM方法:
insertBefore(newChild, referenceNode)
将 newChild
节点插入到 referenceNode
之前。记住,应该对 newChild
的目标父节点调用该方法。 replaceChild(newChild, oldChild)
用 newChild
节点替换 oldChild
节点。 removeChild(oldChild)
从运行该方法的节点中删除 oldChild
节点。 appendChild(newChild)
将 newChild
添加到运行该函数的节点之中。newChild
被添加到目标节点孩子列表中的末端。 hasChildNodes()
在调用该方法的节点有孩子时则返回 true,否则返回 false。 hasAttributes()
在调用该方法的节点有属性时则返回 true,否则返回 falseExample
// These first two lines get the DOM tree for the current Web page,
// and then the <html> element for that DOM tree
var myDocument = document;
var htmlElement = myDocument.documentElement;
// What's the name of the <html> element? "html"
alert("The root element of the page is " + htmlElement.nodeName);
// Look for the <head> element
var headElement = htmlElement.getElementsByTagName("head")[0];
if (headElement != null) {
alert("We found the head element, named " + headElement.nodeName);
// Print out the title of the page
var titleElement = headElement.getElementsByTagName("title")[0];
if (titleElement != null) {
// The text will be the first child node of the <title> element
var titleText = titleElement.firstChild;
// We can get the text of the text node with nodeValue
alert("The page title is '" + titleText.nodeValue + "'");
}
// After <head> is <body>
var bodyElement = headElement.nextSibling;
while (bodyElement.nodeName.toLowerCase() != "body") {
bodyElement = bodyElement.nextSibling;
}
// We found the <body> element...
// Remove all the top-level <img> elements in the body
if (bodyElement.hasChildNodes()) {
for (i=0; i<bodyElement.childNodes.length; i++) {
var currentNode = bodyElement.childNodes[i];
if (currentNode.nodeName.toLowerCase() == "img") {
bodyElement.removeChild(currentNode);
}
}
}
}
- DOM学习1
- DOM学习笔记1:初识DOM
- DOM学习---DOM对象
- ajax学习之1---Dom
- Javascript DOM学习笔记1
- DOM 学习笔记(1)
- javaScript学习DOM案例1
- 学习笔记(1)Dom
- JavaScript DOM学习(1)
- DOM学习笔记(1)
- Dom学习
- Dom学习
- DOM学习
- DOM学习
- DOM学习
- DOM学习
- Dom学习
- dom学习
- A new life, new day, I started my life in Shanghai
- SOA和BPM结合的三种方法
- SEO八方解码——让随心告诉你鲜为人知的SEO新应用(一)
- SEO八方解码——让随心告诉你鲜为人知的SEO新应用(一)
- POPUP_TO_SELECT_MONTH
- DOM学习1
- 对于oracle进行简单树查询(递归查询)
- dfgdf
- 为什么要使用委托
- 网页加载GIF
- 韩国新闻网报导说,网路民意调查显示:八成三韩国民众渴望与中国一战以洗涤韩战耻辱。
- ddd
- Javascript收藏(一)
- 我的自述