How do I select text nodes with jQuery?

前端 未结 11 1564
独厮守ぢ
独厮守ぢ 2020-11-21 04:30

I would like to get all descendant text nodes of an element, as a jQuery collection. What is the best way to do that?

11条回答
  •  误落风尘
    2020-11-21 05:05

    Jauco posted a good solution in a comment, so I'm copying it here:

    $(elem)
      .contents()
      .filter(function() {
        return this.nodeType === 3; //Node.TEXT_NODE
      });
    

提交回复
热议问题