Remove ' ' - still trying

2019-03-12 02:48发布

Still looking for a way to delete ' ' from my html code, found number of ways on stackoverlow.com, but neither of those seam to work!

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 - playground http://jsfiddle.net/MrTest/hbvjQ/85/

Any help much appreciated.
Pete

7条回答
2楼-- · 2019-03-12 03:35

You are replaceing the text, but not applying it back. In addition, use $.trim to get rid of the &nbsp;

$('p').text(function(i,e){
       return $.trim(e.replace(/ /g, ''));
    });
查看更多
登录 后发表回答