在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写法的更多相关文章

  1. firstChild,lastChild,nextSibling,previousSibling & 兼容性写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. HTML DOM firstChild lastChild nextSibling previousSibling 属性_获取属性值的undefined问题

    <html> <head> <title>HTML示例</title> <style type="text/css"> ...

  3. JS中firstChild,lastChild,nodeValue属性

    childNodes 在JavaScript中,使用childNodes属性可以返回一个数组,这个数组包含给定元素节点的全体子节点. firstChild firstChild 这句代码等价于 目标元 ...

  4. 一个兼容 node 与浏览器的模块写法

    一个兼容 node 与浏览器的模块写法 // test.js (function (root, factory) { if (typeof define === 'function' &&am ...

  5. 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)结果如下

  6. js firstChild 、nextSibling、lastChild、previousSibling、parentNode

    nextSibling下一个兄弟节点 previousSibling上一个兄弟 parentNode父亲节点 <select><option value="zs" ...

  7. 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 ...

  8. 【CSS3】---结构性伪类选择器-first-child+last-child

    结构性伪类选择器—first-child “:first-child”选择器表示的是选择父元素的第一个子元素的元素E.简单点理解就是选择元素中的第一个子元素,记住是子元素,而不是后代元素. 示例演示 ...

  9. [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 ...

随机推荐

  1. cocos2dx游戏开发——微信打飞机学习笔记(一)——开发准备

    一.环境的搭建 1.Windows开发准备: (1)软件下载及安装 •下载Cocos2d-x 最新版本:http://www.cocos2d-x.org/download 或者从Cocos2d-x G ...

  2. LR Analysis:详解FirstBufferTime

    LR Analysis:详解FirstBufferTime   详解 第 一次缓冲时间 测试结果分析过程中,经常遇到第一次缓冲时间 FirstBufferTime,并且发现大 部分系统的响应时间也都浪 ...

  3. Loadrunner的自定义监控器

    Loadrunner的自定义监控器 可以使用lr_user_data_point()来实现自定义监控,下面是一个小例子: double showsomething(); Action(){ doubl ...

  4. Maven项目在Eclipse中调试 Debug

    废话不说一路跟图走. 断点会进入到如下页面点击Edit Source Lookup Path 如下图操作 成功进入Debug模式

  5. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  6. poj 2774 Long Long Message 后缀数组基础题

    Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 24756   Accepted: 10130 Case Time Limi ...

  7. VS链接过程中与MSVCRT.lib冲突

    vs代码生成有/MT,/MTd,/Md,/MDd四个编译选项,分别代表多线程.多线程调试.多线程DLL.多线程调试DLL. 编译时引用的lib分别为libcmt.li.libcmtd.lib.msvc ...

  8. LIS(n^2) POJ 2533 Longest Ordered Subsequence

    题目传送门 题意:LIS(Longest Increasing Subsequence)裸题 分析:状态转移方程:dp[i] = max (dp[j]) + 1   (a[j] < a[i],1 ...

  9. iOS Outlets Referencing Outlets

    一位网友的解释: 原址:http://www.cocoachina.com/bbs/read.php?tid-21295.html

  10. POJ3469 Dual Core CPU(最小割)

    形象生动的最小割.. #include<cstdio> #include<cstring> #include<queue> #include<algorith ...