jol*_*olt 24 html javascript css regex jquery
让我们假设以下元素(查找尾随和前导空格):
<p>
<span class="item">Lorem Ipsum is simply dummy text </span><span class="item">of the printing and typesetting</span><span class="item"> industry.</span>
</p>
Run Code Online (Sandbox Code Playgroud)
由于display: inline-block此处显示的奇怪行为,我想替换所有空格:http://jsfiddle.net/SQuUZ/(关于所有浏览器的dunno,但最新的Chrome和Firefox都行为相同).
现在,由于javascript是一个选项,jQuery也是如此,我可以:
$('p').text($('p').text().replace(/ /g, ' '));
Run Code Online (Sandbox Code Playgroud)
但是它逃脱了 并且变成了a mess of entities.
显然,出于这样的目的,我们可以使用$('p').html():
$('p').html($('p').html().replace(/ /g, ' '));
Run Code Online (Sandbox Code Playgroud)
但是这个更糟糕,因为它还 在标签内添加:
<p>
<span class="item">Lorem Ipsum is simply dummy text </span><span class="item">of the printing and typesetting</span><span class="item"> industry.</span>
</p>
<!-- TL;DR -->
<span class="item"></span> <!-- is actually invalid... -->
Run Code Online (Sandbox Code Playgroud)
它打破了一切......
<span>元素item(也可能不总是<p>).我有什么选择?
事实上,任何人都可以解释为什么多线/单线存在这样的错误display: inline-block;?(参见上面的小提琴链接,并检查......)
gdo*_*ica 40
$('p').contents().filter(function(){
return this.nodeType == 3 // Text node
}).each(function(){
this.data = this.data.replace(/ /g, '\u00a0');
});
Run Code Online (Sandbox Code Playgroud)
Qta*_*tax 19
即使是jQuery真的很棒并做所有事情,CSS在某些情况下也可以工作:
white-space: pre-wrap;
Run Code Online (Sandbox Code Playgroud)
演示.
CSS3相关: text-space-collapse
| 归档时间: |
|
| 查看次数: |
54359 次 |
| 最近记录: |