<body>
<ul id="ul1">
<li>11111</li>
<li>22222</li>
<li>33333</li>
<li>44444</li>
</ul>
</body>

1,firstChild:第一个子节点。

  标准下:firstChild会包含文本类型的节点

  非标准下(IE7以下):fistChild只包含元素节点

  var oUl = document.getElementById('ul1');

alert( oUl.firstChild ); //标准下显示[object Text],非标准下是[object HTMLLIElement]
    alert( oUl.firstElementChild )//标准和非标准都是 [object HTMLLIElement]

  注意兼容性:

   var oFirst = oUl.firstElementChild || oUl.firstChild;
       oFirst.style.background = 'red';

2,lastChild:最后一个子节点(同上)

  兼容性:

      var oLast = oUl.lastElementChild || oUl.lastChild;
      oLast.style.background = 'yellow';

3,nextSibling:下一个兄弟节点

      var oNext = oFirst.nextElementSibling || oFirst.nextSibling;
      oNext.style.background = 'blue';4,previousSibling:上一个兄弟节点

  var oPrev = oLast.previousElementSibling || oLast.previousSibling;
      oPrev.style.background = 'orange';

JS-firstChild,firstElementChild,lastChild,firstElementChild,nextSibling,nextElementSibling的更多相关文章

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

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

  2. nodeValue、firstChild和lastChild属性

    nodeValue属性如果想改变一个文本节点的值,那就使用DOM提供的nodeValue属性,他用来得到(和设置)一个节点的值:node.nodeValue但是有个细节必须注意:在用nodeValue ...

  3. 选择器:first-child与:last-child失效的解决方法

    作为还在努力练习的代码小白来说,有时类名或者ID名太多很容易就会搞混,为此,在练习中会想着借用多样的选择器来设置而不是每一个标签都设一个类名(Id名),在此次练习中使用选择器:first-child与 ...

  4. CSS nth-child、first-child、last-child、nth-of-type、first-of-type和last-of-type选择器使用

    以下示例主要讲解nth-child.first-child.last-child.nth-of-type.first-of-type和last-of-type使用. 示例代码: <!DOCTYP ...

  5. js node.children与node.childNodes与node.firstChild,node,lastChild之间的关系

    博客搬迁,给你带来的不便,敬请谅解! http://www.suanliutudousi.com/2017/11/06/js-node-children%e4%b8%8enode-childnodes ...

  6. jQuery中的子(后代)元素过滤选择器(四、六):nth-child()、first-child、last-child、only-child

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  7. CSS3中first-child、last-child、nth-child、nth-last-child

    1.单独指定第一个子元素.最后一个子元素的样式 <style type="text/css"> li:first-child{ background:yellow; } ...

  8. CSS选取指定位置标签first-child、last-child、nth-child

    1.first-child 选择列表中的第一个标签. 2.last-child 选择列表中的最后一个标签 3.nth-child(n) 选择列表中的第n个标签 4.nth-child(2n) 选择列表 ...

  9. first-child和last-child选择器 nth-child(n)第几个元素 nth-last-child(n)倒数第几个元素

    :first-child 和  :last-child 分别表示父元素中第一个 或者  最后一个 子元素设置样式,如上图

随机推荐

  1. UCS2和UTF16有区别

    UCS2是定长的,固定2个字节,所以不能支持扩展字符,而UTF16是变长的.   UCS2是落伍的.   msdn里有这样一段描述: UCS-2 is a predecessor of UTF-16. ...

  2. VFP自定义函数StringFormat (仿.NET String.Format 方法)

    VFP仿.NET String.Format 方法 将指定字符串中的每个{x}替换为相应值,并返回文本 *-- 调用格式 StringFormat("日期{2},字符{1}",&q ...

  3. try--catch--finally中return返回值执行的顺序(区别)

    1.try块中没有抛出异常,try.catch和finally块中都有return语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static int ...

  4. 身份证校验,前台js校验,后台java校验

    js校验: var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内 ...

  5. SQLServer2008R2 error 40解决方法

    实际遇到的问题,以下为搜到的解决方案,亲测可用 转自 http://blog.csdn.net/laga516/article/details/7696577 最近一直在配置服务器, 这当中最头疼的就 ...

  6. 手机页面touch触摸事件

    请看示例: <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset=" ...

  7. include使用中注意的问题

    发现include文件不必添加,但是一定要放在工程路径下,不然就找不到,或者也可以在文件上写出完整的路径. 可以修改路径为'include "../src/enc_defines.v&quo ...

  8. SQL Server 2008 通用分页存储过程

    1.alert USE [数据库名称] GO /****** Object: StoredProcedure [dbo].[dbTab_PagerHelper] Script Date: 08/22/ ...

  9. shell编程之流程控制

    -d 判断该文件是否存在,并且是否为目录文件 -e 判断该文件是否存在 -f 判断该文件是否存在,并且是否为普通文件 形式 [ -e   /home/cao/test.txt ] -r 文件 判断该文 ...

  10. console.log()与alert()的区别

    1.alert() a.有阻塞作用,不点击确定,后续代码无法继续执行 b.alert只能输出string,如果alert输出的是对象,会自动调用toString()方法 eg:alert([1,2,3 ...