How to get text inside of container that is not part of children

后端 未结 3 1131

Let\'s have html code like this:

1.Hallo Kitty,
How are you?
<
3条回答
  •  孤街浪徒
    2021-01-06 06:26

    this will work for you

    $('#d1')
      .contents()
      .filter(function() {
        return this.nodeType == Node.TEXT_NODE;
    }).text()
    

    or you can use this as suggested below for old browser support also

    var text = $("#d1").contents().filter( function() {
        return this.nodeType === 3;
    }).text();
    

    Demo

提交回复
热议问题