V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
just1
V2EX  ›  JavaScript

如何获取父节点的上一兄弟节点

  •  
  •   just1 · 2016-01-28 20:52:16 +08:00 · 2568 次点击
    这是一个创建于 3016 天前的主题,其中的信息可能已经有所发展或是发生改变。

    找找文档,应该是 this.parentNode.previousSibling
    但是返回的不是对象而是 text...
    怎么获取父节点的上一兄弟节点呢

    8 条回复    2016-01-29 23:24:20 +08:00
    just1
        1
    just1  
    OP
       2016-01-28 21:01:10 +08:00
    0
    Keita1314
        2
    Keita1314  
       2016-01-28 21:12:31 +08:00   ❤️ 1
    previousElementSibling 而不是 previousSibling
    Keita1314
        3
    Keita1314  
       2016-01-28 21:16:52 +08:00   ❤️ 1
    Notes
    Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained, for example, using Node.firstChild or Node.previousSibling may refer to a whitespace text node rather than the actual element the author intended to get.

    See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.
    just1
        4
    just1  
    OP
       2016-01-28 21:29:16 +08:00
    @Keita1314 非常感谢
    Roycom
        5
    Roycom  
       2016-01-28 22:09:37 +08:00
    mark
    pysama
        6
    pysama  
       2016-01-28 22:34:56 +08:00   ❤️ 1
    @just1
    对于不支持 previousElementSibling 的浏览器。循环获取 previousSibling ,直到其 nodeType 为 1
    westpsk
        7
    westpsk  
       2016-01-28 22:38:12 +08:00
    我只想问为什么要用裴博士作为头像
    just1
        8
    just1  
    OP
       2016-01-29 23:24:20 +08:00 via Android
    @westpsk 当初要传头像,刚好这张图片
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   812 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:29 · PVG 06:29 · LAX 15:29 · JFK 18:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.