How do I select text nodes with jQuery?

前端 未结 11 1530
独厮守ぢ
独厮守ぢ 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:21

    I had the same problem and solved it with:

    Code:

    $.fn.nextNode = function(){
      var contents = $(this).parent().contents();
      return contents.get(contents.index(this)+1);
    }
    

    Usage:

    $('#my_id').nextNode();
    

    Is like next() but also returns the text nodes.

提交回复
热议问题