Find text between two tags/nodes

后端 未结 6 1066
北荒
北荒 2021-02-20 00:32

I thought that this would be rather straightforward, but I think the keywords are just too general so I keep getting query results for things like this and this.

Basical

6条回答
  •  甜味超标
    2021-02-20 00:56

    Dropping down to the DOM lets you see text node contents when checking siblings.

    Something like:

    function combineSpans(span, nextspan)
    {
      var follower = span.nextSibling;
      var concat = true;
    
       while (follower != nextspan)
       {
         if (follower.nodeName != '#text')
         {
           concat = false;
           break;
         }
    
         var len = follower.data.trim().length;
         if (len > 0)
         {
           concat = false;
           break;
         }
    
         follower = follower.nextSibling;
       }
    
      if (concat)
      {
        $(span).text($(span).text() + " " + $(follower).text());
        $(follower).remove();
      }
    }
    

    Using this with your HTML in this CodePen.

提交回复
热议问题