javascript每日一练(三)——DOM一
一、Dom基础
childNodes(有兼容问题),children nodeType getAttribute() firstChild,lastChild,previousSilbing,nextSilbing(有兼容问题)
offsetParent,parentNode
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function(){
var oUl = document.getElementById('ul1');
var aLi = oUl.childNodes;
//1.childNodes
//alert(oUl.childNodes.length); //chrome 7 ie 3 有兼容问题,标准浏览器下会获取包括文本节点
//2.nodeType: 1,元素节点 3,文本节点
/*for(var i=0;i<aLi.length;i++){
if(aLi[i].nodeType==1){
aLi[i].style.background = 'red';
}
}*/
//3.children 无兼容问题
//alert(oUl.children.length); //3
//4.Dom方式获取元素属性
//alert(oUl.getAttribute('id'));
//5.firstChild lastChild 有兼容问题
//alert(oUl.firstChild); //chrome:object Text ie:object HTMLElement
//var oFirst = oUl.firstElementChild || oUl.firstChild;
//oFirst.style.background = 'red';
//6.兄弟节点 有兼容问题(同上) nextSilbing nextElementSilbing previousSilbing previousElementSilbing
//oUl.nextSibling.style.background = 'red';
//7.offsetParent获取元素基于定位的父级
/*oUl.onclick = function(){
alert(this.offsetParent.tagName); //body
};*/
//8.parentNode 获取元素的父节点
//alert(oUl.parentNode.tagName); //body
//9.getByClass
var aBox = getByClass(document.body, 'box');
for(var i=0;i<aBox.length;i++){
aBox[i].style.background = 'red';
}
};
function getByClass(oParent, sClass){
var aEle = oParent.getElementsByTagName('*');
var aResult = [];
var i = 0;
for(i=0;i<aEle.length;i++){
if(aEle[i].className == sClass){
aResult.push(aEle[i]);
}
}
return aResult;
}
</script>
</head>
<body>
<p>p1</p>
<ul id="ul1">
<li></li>
<li class="box"></li>
<li></li>
</ul>
<p class="box">p2</p>
</body>
</html>
javascript每日一练(三)——DOM一的更多相关文章
- javascript每日一练(四)——DOM二
一.DOM的创建,插入,删除 createElement(标签名) appendChild(节点) insertBefore(节点,原有节点) removeChild(节点) <!doctype ...
- javascript每日一练(八)——事件三:默认行为
一.阻止默认行为 return false; 自定义右键菜单 <!doctype html> <html> <head> <meta charset=&quo ...
- javascript每日一练(一)——javascript基础
一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...
- javascript每日一练(十三)——运动实例
一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- javascript每日一练(五)——BOM
一.BOM打开,关闭窗口 window.open(); window.close(); <!doctype html> <html> <head> <meta ...
- javascript每日一练—运动
1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十四)——弹性运动
一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十二)——运动框架
运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...
- javascript每日一练(十一)——多物体运动
一.多物体运动 需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用: 例子1: <!doctype html> <html> <h ...
随机推荐
- CURL采集
<?php $url='';//输入'网址 $ch = curl_init(); $timeout = 5;//超时时间 curl_setopt ($ch, CURLOPT_URL, $url) ...
- python 爬一下
1.安装Requestswindow:pip install requestslinux:sudo pip install requests国内安装缓慢,建议到:http://www.lfd.uci. ...
- SWIFT学习笔记01
1.Swift.用来推断option是不是nil,相当于OC的 if(option) if let name = option{ greeting = "if=====" }els ...
- 【JavaScript】history.back() 网页已过期
使用history.back()进行返回时,有时会提示“网页已过期”, 多数是因为目标页面的form为post提交方式,而且是表单已经提交后的响应页面,无法找到form中的具体参数,而“报错”. 具体 ...
- vsftpd安装
原文连接:史上最详细的vsftpd配置文件讲解: http://www.9170.org/post-322.html 参考博文:centos启用ftp功能:http://os.51cto.com/ar ...
- eclipse+tomcat+maven debug的时候总是出现source not found /Edit lookup path...的问题解决方案
eclipse+tomcat+maven debug的时候总是出现source not found /Edit lookup path...的问题解决方案 这个问题纠结好久好久.... 问题出现的环 ...
- POJ 3047 Fibonacci
DEBUG很辛苦,且行, 且珍惜 原代码: ans[0][0] = (ans[0][0] * a[flag][0][0] + ans[0][1] * a[flag][1][0]) % 10000; a ...
- Flask web开发 简单介绍
Flask是一个基于python的轻量级web框架.当安装好后Flask后 (pip install flask),就可以开始使用了. 一.最简单的例子 1.新建目录,作为web应用的目录,如: mk ...
- solrCloud+tomcat+zookeeper配置
一.环境准备: Solr版本:4.7.0 下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/4.7.0 Tomcat版本:6.0.39 下载地 ...
- 关于DataGridViewComboBoxCell修改后提交数据源
最近在项目遇到一个功能实现.是在DataGridView中DataGridViewComboboxColumn列绑定数据源, DisplayMember为数据表的Name列,ValueMember是数 ...