html和js中对“空格”的使用
1.
转义字符的写法。
在html标签中使用。
可以写多个,每有一个则会渲染出一个空格,不会像按多个空格键一样,最终只显示一个。
<div>1 2</div> // 1 2
<div>1 2</div> // 1 2注意:&和结尾的;都不能少
2. 
ASCII编码的写法。
在html标签中使用。
写一个和同时写多个一样,最终只显示一个,类似于按空格键
<div>1 2</div> // 1 2
<div>1 2</div> // 1 2console.log(1+ String.fromCharCode(32) + String.fromCharCode(32) + String.fromCharCode(32) +2) // 1 23.\xa0
\xa0属于latin(ISO/IEC_8859-1,拉丁字母)中的扩展字符集字符,代表空白符nbsp(non-breaking space)
在html标签中使用。
和 一样,可以写多个,显示多个
<div>1 2</div> // 1 2console.log(1+ '\xa0\xa0\xa0\xa0' +2) //1 24.U+0020
属于Unicode字符
在js中使用。用法和\xa0一样
console.log(1+ '\u0020\u0020\u0020\u0020' +2) // 1 2
5.\x20
标准键盘码值表-十六进制
在html标签中使用。只显示一个
<div>1 2</div> // 1 2console.log(1+ '\x20\x20\x20\x20' +2) // 1 26.%20
console.log(1+ decodeURIComponent('%20')+decodeURIComponent('%20')+decodeURIComponent('%20') +2) // 1 27.\t
console.log(1+ '\t\t\t\t' +2) // 1 2本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!