删除' '
我正在寻找一种从我的 html 代码中删除 ' '
的方法,在 stackoverflow.com 上找到了多种方法,但这些方法都不起作用!
HTML
<p>No Space</p>
<p> 1 Space</p>
<p> 2 Spaces</p>
<p> 3 Spaces</p>
<p> 4 Spaces</p>
jQuery
$(document).ready(function() {
$('p').text().replace(/ /g, '');
//$('p').html($(this).html().replace(/ /gi,''));
});
jsfiddle - 游乐场 http://jsfiddle.net/MrTest/hbvjQ/85/
I'm looking for a way to delete ' '
from my html code, found number of ways on stackoverflow.com, but neither of those seam to work!
HTML
<p>No Space</p>
<p> 1 Space</p>
<p> 2 Spaces</p>
<p> 3 Spaces</p>
<p> 4 Spaces</p>
jQuery
$(document).ready(function() {
$('p').text().replace(/ /g, '');
//$('p').html($(this).html().replace(/ /gi,''));
});
jsfiddle - playground http://jsfiddle.net/MrTest/hbvjQ/85/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的代码中有 而不是
http:// /jsfiddle.net/genesis/hbvjQ/76/
You have in your code instead of
http://jsfiddle.net/genesis/hbvjQ/76/
这个将替换每个空白字符:
或者如果您只想替换不间断空格:
jsFiddle 演示
我使用 闭包作为
.text() 的参数来设置新值
。
请注意,HTML 实体最后需要结束
;
。This one will replace every white-space character:
Or if you only want to replace non-breaking spaces:
jsFiddle Demo
I am setting the new value using a closure as a parameter for
.text()
.Please note that HTML entities need a closing
;
in the end.这是一个非 jQuery 的答案,因为使用 jQuery 来完成这样的任务是多余的,除非您已经将它用于网站上的其他内容:
Here's a non-jQuery answer, since using jQuery for such a task is overkill unless you're already using it for something else on your site:
尝试一下
,或者如果您想删除 尝试
,还请注意空格是
而不是 (您丢失了;)
try
or if you wish to delete the try
also please note that space is
and not (you are missing ;)
根据 bažmegakapa' 答案,这可以用于包含其他元素的元素。
.text()
删除 html 元素;.html()
没有Based on bažmegakapa' answer, this can be used on elements containing other elements.
.text()
gets rid of html elements;.html()
does not这是代码:
这是jsfiddle: http://jsfiddle.net/hbvjQ/62/
Here is the code:
And here is jsfiddle: http://jsfiddle.net/hbvjQ/62/