Javascript DOM基础(二) childNodes、children
childNodes知识点:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function() { var oUl = document.getElementById('ul1'); /*
元素.childNodes : 只读 属性 子节点列表集合
标准下:包含了文本和元素类型的节点,也会包含非法嵌套的子节点
非标准下:只包含元素类型的节点,ie7以下不会包含非法嵌套子节点 childNodes只包含一级子节点,不包含后辈孙级以下的节点 DOM节点的类型有很多种 12种 元素.nodeType : 只读 属性 当前元素的节点类型 元素节点 : 1
属性节点 : 2
文本节点 : 3
注释节点 : 8
文档节点 : 9
*/ alert( oUl.childNodes.length ); //alert( oUl.nodeType ); //alert(oUl.childNodes[0].nodeType); //属性
// 元素.attributes : 只读 属性 属性列表集合 //alert( oUl.attributes.length ); //alert( oUl.attributes[0].nodeType ); for (var i=0; i<oUl.childNodes.length; i++) { if ( oUl.childNodes[i].nodeType == 1 ) {
oUl.childNodes[i].style.background = 'red';
} } }
</script>
</head> <body>
<ul id="ul1" style="border: 1px solid red;">
<li>11111 <span>span</span></li>
<li>22222</li>
<li>33333</li>
<li>44444</li>
<p>pppppppp</p>
</ul>
</body>
</html>
children知识点:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function() { var oUl = document.getElementById('ul1'); /*
元素.children : 只读 属性 子节点列表集合
标准下:只包含元素类型的节点
非标准下:只包含元素类型的节点
*/ //alert( oUl.children.length ); for (var i=0; i<oUl.children.length; i++) { oUl.children[i].style.background = 'red'; } }
</script>
</head> <body>
<ul id="ul1" style="border: 1px solid red;">
<li>11111 <span>span</span></li>
<li>22222</li>
<li>33333</li>
<li>44444</li>
<p>pppppppp</p>
</ul>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function() { var oUl = document.getElementById('ul1');
/*
元素.firstChild : 只读 属性 第一个子节点
标准下:firstChild会包含文本类型的节点
非标准下:只包含元素节点
元素.firstElementChild : 只读 属性 标准下获取第一个元素类型的子节点
*/ //alert( oUl.firstChild ); //alert( oUl.firstElementChild ); /*if ( oUl.firstElementChild ) {
oUl.firstElementChild.style.background = 'red';
} else {
oUl.firstChild.style.background = 'red';
}*/ var oFirst = oUl.firstElementChild || oUl.firstChild;
oFirst.style.background = 'red'; /*
元素.lastChild || 元素.lastElementChild 最后一个子节点
*/
var oLast = oUl.lastElementChild || oUl.lastChild;
oLast.style.background = 'yellow'; /*
元素.nextSibling || 元素.nextElementSibling 下一个兄弟节点
*/
var oNext = oFirst.nextElementSibling || oFirst.nextSibling;
oNext.style.background = 'blue'; /*
元素.previousSibling || 元素.previousElementSibling 上一个兄弟节点
*/
var oPrev = oLast.previousElementSibling || oLast.previousSibling;
oPrev.style.background = 'orange'; }
</script>
</head> <body>
<ul id="ul1">
<li>11111</li>
<li>22222</li>
<li>33333</li>
<li>44444</li>
</ul>
</body>
</html>
应用 实例:
1.留言板加强
最多只能添加5(自己定)条,最先留的言会被清除
2.为留言添加批量删除
可以点击选择单个留言,点击批量删除按钮,可以删除被选中的留言内容
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function() { var oText = document.getElementById('text1');
var oBtn = document.getElementById('btn');
var oBtn2 = document.getElementById('btn2');
var oUl = document.getElementById('ul1'); oBtn.onclick = function() { /*
document.createElement(标签名称); 创建元素
*/ var oLi = document.createElement('li'); //oLi.innerHTML = oText.value + '<a href="javascript:;">删除</a>';
oLi.innerHTML ='<input type="checkbox" value="" />'+ oText.value; var oA = document.createElement('a');
oA.innerHTML = '删除';
oA.href = 'javascript:;';
oA.onclick = function() { /*
父级.removeChild(要删除的元素); 删除元素
*/ oUl.removeChild( this.parentNode ); } oLi.appendChild( oA ); //添加到页面中
/*
父级.appendChild(要添加的元素) 方法 追加子元素
*/
//oUl.appendChild( oLi ); /*
父级.insertBefore(新的元素,被插入的元素) 方法 在指定元素前面插入一个新元素
在ie下如果第二个参数的节点不存在,会报错
在其他标准浏览器下如果第二个参数的节点不存在,则会以appendChild的形式进行添加
*/
//oUl.insertBefore( oLi, oUl.children[0] ); if ( oUl.children[0] ) {
oUl.insertBefore( oLi, oUl.children[0] );
if(oUl.children.length>5){
oUl.removeChild( oUl.children[oUl.children.length-1]);
};
} else {
oUl.appendChild( oLi );
}; var aInput=oUl.getElementsByTagName("input"); for(var i=0;i<aInput.length;i++){ aInput[i].index=i;
aInput[i].onOff=true;//为当前表单元素添加一个开关
aInput[i].onclick=function(){ if(aInput[this.index].onOff){//如果当前开关为true
aInput[this.index].value='checkBox';//就为当前 表单 的value值添加check
aInput[this.index].onOff=false;
}else{
aInput[this.index].value='';
aInput[this.index].onOff=true;
};
};
}; oBtn2.onclick=function(){
for(var i=0;i<aInput.length;i++){
if(aInput[i].value=='checkBox'){
oUl.removeChild( aInput[i].parentNode );
};
};
}; }; }
</script>
</head> <body>
<input type="text" id="text1" /><input type="button" value="留言" id="btn" />
<ul id="ul1"></ul>
<input type="button" value="删除" id="btn2" />
</body>
</html>
Javascript DOM基础(二) childNodes、children的更多相关文章
- JavaScript DOM 基础操作
JavaScript DOM 基础操作 一.获取元素的六方式 document.getElementById('id名称') //根据id名称获取 document.getElementsByclas ...
- JavaScript Dom基础-9-Dom查找方法; 设置DOM元素的样式; innerHTML属性的应用; className属性的应用; DOM元素上添加删除获取属性;
JavaScript Dom基础 学习目标 1.掌握基本的Dom查找方法 domcument.getElementById() Domcument.getElementBy TagName() 2.掌 ...
- JavaScript DOM基础总结
上个月在进行百度三面时候,面试官提问JavaScript DOM方法,我回答的有点少,前面太关注JavaScript 兼容性,框架方面,JavaScript 原生DOM基础没有记牢,心中有点遗憾.下来 ...
- Javascript DOM基础(一)概念
Dom基础概念: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...
- JavaScript(DOM编程二)
文档加载完毕之后,在Window.onload方法中创建元素节点,添加到DOM文档中 代码演示: <html> <head lang="en"> <m ...
- javascript DOM基础操作
DOM(Document Object Model)即文档对象模型,针对HTML和XML文档的API(应用程序接口).DOM描绘了一个层次化的节点树,运行开发人员添加.移除和修改页面的某一部分.DOM ...
- JS基础---->javascript的基础(二)
记载javascript的一些基础的知识.我们在春风秋雨中无话不说,又在春去秋来中失去了联系. js中string类型 一.字符方法:charAt() 和 charCodeAt() var strin ...
- JavaScript Dom基础
一.DOM查找 1.document.getElementById("id") -功能:返回对拥有指定ID的第一个对象的引用 -返回值:DOM对象 -说明:id为DOM元素上id属 ...
- JavaScript学习 - 基础(二) - 基础类型/类型转换
基础类型 - 数字类型(Number) 1.最基本的数据类型 2.不区分整型数值和浮点型数值 3.所有数字采用64位浮点格式存储,相当于Java和C语言中double格式 4.能表示的最大值 +- 1 ...
随机推荐
- python学习之while语句
while循环 1.简单的while循环while True: ")#这是一个简单的while循环,当等于True时会一直打印1 2.while执行多少次后退出 coun=0while Tr ...
- Pet Shop4.0
http://blog.csdn.net/RainyLin/article/details/1769947
- MAC地址泛洪攻击测试
测试环境:kali系统(2个kali分别作攻击人和目标用户) win7系统(主机) 1.步配置FTP设置用户名密码 2.在攻击kali端测试网络的连通性 3.测试tpf是否正常 开始泛洪 4.开始抓包 ...
- GZFramwork快速开发框架演练之会员系统(三)添加会员等级管理
1.设计会员等级表结构 创建语句: from sysobjects where id = object_id('tb_MembersLevel') and type = 'U') drop table ...
- [Selenium] 拖拽一个 Component 到 Workspace
先使Component可见,获取Component位置信息,获取Workspace位置信息,点击Component并拖拽到Workspace,最后释放.(调试时dragAndDropOffset()方 ...
- mysql数据库编码问题
http://www.cnblogs.com/sunzn/archive/2013/03/14/2960248.html 修改/etc/my.cnf 添加: [mysql] default-chara ...
- (转)SQLServer实例讲解
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- 【linux】linux下yum安装后Apache、php、mysql默认安装路径
原文:http://blog.csdn.NET/u010175124/article/details/27322757apache:如果采用RPM包安装,安装路径应在 /etc/httpd目录下apa ...
- 带事物处理的DBHelp和sql语句
DBHelp语句 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- JDK配置
一.下载和安装 二.配置环境变量 1.计算机→属性→高级系统设置→高级→环境变量 2.系统变量→新建JAVA_HOME,值为jdk的安装目录(如C:\Java\jdk1.7.0) 3.系统变量→修改P ...