兼容的firstChild,lastChild,nextSibling,previousSibling写法
在IE下是支持firstChild,lastChild,nextSibling,previousSibling
但是在FF下,由于它会把标签之间的空格当成文本节点,所以为了准确地找到相应的元素,会用
firstElementChild,
lastElementChild,
nextElementSibling,
previousElementSibling
兼容的写法是这样的
var oFirst = oParent.firstElementChild||oParent.firstChild
也可以这么写
var oFirst = oParent.children[0];
var oLast = oParent.lastElementChild||oParent.lastChild
也可以这么写
var oLast = oParent.children[oParent.children.length-1];
var oNext = obj.nextElementSibling||obj.nextSibling
var oPre = obj.previousElementSibling||obj.previousSibling
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload = function() {
var oUl = document.getElementById('ul1'); /*
元素.lastChild || 元素.lastElementChild 第一个子节点
元素.lastChild || 元素.lastElementChild 最后一个子节点
元素.nextSibling || 元素.nextElementSibling 下一个兄弟节点
元素.previousSibling || 元素.previousElementSibling 上一个兄弟节点 */
var oFirst = oUl.firstElementChild || oUl.firstChild;
//var oFirst = oUl.children[0];
oFirst.style.background = 'red'; var oLast = oUl.lastElementChild || oUl.lastChild;
//var oLast = oUl.children[oUl.children.length-1];
oLast.style.background = 'yellow'; var oNext = oFirst.nextElementSibling || oFirst.nextSibling;
oNext.style.background = 'blue'; 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>
兼容的firstChild,lastChild,nextSibling,previousSibling写法的更多相关文章
- firstChild,lastChild,nextSibling,previousSibling & 兼容性写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML DOM firstChild lastChild nextSibling previousSibling 属性_获取属性值的undefined问题
<html> <head> <title>HTML示例</title> <style type="text/css"> ...
- JS中firstChild,lastChild,nodeValue属性
childNodes 在JavaScript中,使用childNodes属性可以返回一个数组,这个数组包含给定元素节点的全体子节点. firstChild firstChild 这句代码等价于 目标元 ...
- 一个兼容 node 与浏览器的模块写法
一个兼容 node 与浏览器的模块写法 // test.js (function (root, factory) { if (typeof define === 'function' &&am ...
- nth-child,nth-last-child,after,before,tab-highlight-color,first-child,last-child
nth-child:定义第几个元素或者是奇数或者是偶数,或者满足某个数字倍数的dom的样式 如 li:nth-child(3n),结果如下,li:nth-child(2)结果如下
- js firstChild 、nextSibling、lastChild、previousSibling、parentNode
nextSibling下一个兄弟节点 previousSibling上一个兄弟 parentNode父亲节点 <select><option value="zs" ...
- nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-type,first-child,last-child伪类区别和用法
我将这坨伪类分成三组,第一组:nth-child,nth-last-child,only-child第二组:nth-of-type,nth-last-of-type,第三组:first-of-tpye ...
- 【CSS3】---结构性伪类选择器-first-child+last-child
结构性伪类选择器—first-child “:first-child”选择器表示的是选择父元素的第一个子元素的元素E.简单点理解就是选择元素中的第一个子元素,记住是子元素,而不是后代元素. 示例演示 ...
- [CSS] DOM Hierarchy Pseudo Classes :first-child :last-child :nth-child (demystified)
DOM hierarchy pseudo-classes allow you to style specific elements based on where they fall in the hi ...
随机推荐
- hdu 5833 Zhu and 772002 高斯消元
Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...
- HDU 5791 Two DP
Two Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of ...
- Vue#条件渲染
根据不同的条件,响应不同的事件. https://jsfiddle.net/miloer/zed5p1r3/ 可以用template来包装元素,当然浏览器的最终渲染结果不会包含它.我觉得主要用它来自定 ...
- loj 1210 (求最少的加边数使得图变成强连通)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1210 思路:首先是缩点染色,然后重建并且统计新图中的每个点的入度和出度,于是答案就是m ...
- 聊聊传统oo和js的某些对比——对象/函数/new关键字等
自己的学习记录,写的短点可以以后短时间内理清一些疑惑,看前要求你至少了解js中关于原型链等基本概念,因为文章直接以总结的形式理出知识点,没有去解释一些基本的概念! 1.1.熟记两句话,预预热 1. 函 ...
- view和activity的区别
activity相当于控制部分,view相当于显示部分.两者之间是多对多的关系,所有东西必须用view来显示. viewGroup继承自view,实现了ViewManager,ViewParent接 ...
- 【SSM】拦截器的原理、实现
一.背景: 走过了双11,我们又迎来了黑色星期五,刚过了黑五,双12又将到来.不管剁手的没有剁手的,估计这次都要剁手了!虽然作为程序猿的我,没有钱但是我们长眼睛了,我们关注到的是我们天猫.淘宝.支付宝 ...
- LoadRunner如何监控Tomcat性能
使用LoadRunner做性能测试,一般的直觉是LR只能完成脚本录制和编写模拟用户的请求行为,但是在某些情况下,要监控一些中间件或web服务器的性能时,就不能通过录制脚本来完成了,那么就需要手工来编写 ...
- LoadRunner性能测试执行过程的问题
LoadRunner做性能测试 从设计到分析执行 执行测试并分析调优: 测试中报错的信息解决: 1. Failed to connect to server "域名:80": [1 ...
- Maven项目在Eclipse中调试 Debug
废话不说一路跟图走. 断点会进入到如下页面点击Edit Source Lookup Path 如下图操作 成功进入Debug模式