删除' '

发布于 2024-11-16 16:58:08 字数 740 浏览 6 评论 0原文

我正在寻找一种从我的 html 代码中删除 ' ' 的方法,在 stackoverflow.com 上找到了多种方法,但这些方法都不起作用!

HTML

<p>No Space</p>
<p>&nbsp;1 Space</p>
<p>&nbsp;&nbsp;2 Spaces</p>
<p>&nbsp;&nbsp;&nbsp;3 Spaces</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;4 Spaces</p>

jQuery

$(document).ready(function() {

    $('p').text().replace(/ /g, '');
    //$('p').html($(this).html().replace(/&nbsp;/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

才能让你更想念 2024-11-23 16:58:08

您的代码中有   而不是  

$('p').each(function(){
    $(this).html($(this).html().replace(/ /gi,''));
});

http:// /jsfiddle.net/genesis/hbvjQ/76/

You have   in your code instead of  

$('p').each(function(){
    $(this).html($(this).html().replace(/ /gi,''));
});

http://jsfiddle.net/genesis/hbvjQ/76/

千纸鹤 2024-11-23 16:58:08

这个将替换每个空白字符:

$('p').text(function (i, old) {
    return old.replace(/\s/g, '')
});

或者如果您只想替换不间断空格:

$('p').text(function (i, old) {
    return old.replace(/\u00A0/g, '')
});

jsFiddle 演示

我使用 闭包作为 .text() 的参数来设置新值


请注意,HTML 实体最后需要结束 ;

This one will replace every white-space character:

$('p').text(function (i, old) {
    return old.replace(/\s/g, '')
});

Or if you only want to replace non-breaking spaces:

$('p').text(function (i, old) {
    return old.replace(/\u00A0/g, '')
});

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.

可可 2024-11-23 16:58:08

这是一个非 jQuery 的答案,因为使用 jQuery 来完成这样的任务是多余的,除非您已经将它用于网站上的其他内容:

var p = document.getElementsByTagName('p');

Array.prototype.forEach.call(p, function(el) {
  el.innerHTML = el.innerHTML.replace(/ /gi, '');
});
<p>No Space</p>
<p> 1 Space</p>
<p>  2 Spaces</p>
<p>   3 Spaces</p>
<p>    4 Spaces</p>

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:

var p = document.getElementsByTagName('p');

Array.prototype.forEach.call(p, function(el) {
  el.innerHTML = el.innerHTML.replace(/ /gi, '');
});
<p>No Space</p>
<p> 1 Space</p>
<p>  2 Spaces</p>
<p>   3 Spaces</p>
<p>    4 Spaces</p>

夜访吸血鬼 2024-11-23 16:58:08

尝试一下

$('p').each(function() {
     $(this).html($(this).html().replace(/ /g, ''));
});

,或者如果您想删除   尝试

$('p').each(function() {
      $(this).html($(this).html().replace(' ', ''));
});

,还请注意空格是   而不是   (您丢失了;)

try

$('p').each(function() {
     $(this).html($(this).html().replace(/ /g, ''));
});

or if you wish to delete the   try

$('p').each(function() {
      $(this).html($(this).html().replace(' ', ''));
});

also please note that space is   and not   (you are missing ;)

彼岸花ソ最美的依靠 2024-11-23 16:58:08

根据 bažmegakapa' 答案,这可以用于包含其他元素的元素。

$('p').html(function (i, old) {
    return old.replace(/ /g, '')
});

.text() 删除 html 元素; .html() 没有

Based on bažmegakapa' answer, this can be used on elements containing other elements.

$('p').html(function (i, old) {
    return old.replace(/ /g, '')
});

.text() gets rid of html elements; .html() does not

牵强ㄟ 2024-11-23 16:58:08

这是代码:

$('p').each( function() {
    var elem = $( this );
    elem.html( elem.html().replace( / /g,'' ) );
} );

这是jsfiddle: http://jsfiddle.net/hbvjQ/62/

Here is the code:

$('p').each( function() {
    var elem = $( this );
    elem.html( elem.html().replace( / /g,'' ) );
} );

And here is jsfiddle: http://jsfiddle.net/hbvjQ/62/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文