Does .previousSibling always return the parent's Text node first?

前端 未结 3 1884
孤独总比滥情好
孤独总比滥情好 2021-01-12 15:40

I\'m stuck using native DOM methods (I know, right?) and I have a structure like this:


3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 16:08

    Does .previousSibling always return the parent's text node if it exists?

    No. It returns the immediately preceding sibling. In your case, there is a text node (a new line) immediately preceding the a element, so it returns that.

    If you remove the white space it should work as expected:

    However, that's not a particularly nice solution. See @Esailija's answer for a nicer one!

提交回复
热议问题