Remove every white space between tags using JavaScript

前端 未结 5 1604
你的背包
你的背包 2020-12-30 06:57

I\'m trying to remove white space between tags so that childNodes only contain those tags nodes not the white space nodes too. Here\'s my code :

  • 5条回答
    •  青春惊慌失措
      2020-12-30 07:14

      For most cases, I recommend removing space from:

      • Beginning of document
      • End of document
      • After > character
      • Before < character

      There are two cases I can think of where this will not do what you want, and these are the same two cases that impact the less aggressive solutions above.

      • Empty space between inline-block elements is actually an intended or expected part of the layout. If this space is collapsed to zero characters, the implicit space between elements is removed. This can be avoided by changing my regex below to replace with a " ".

      • My original answer has been updated to preserve whitespace in

    提交回复
    热议问题