最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove   from html - Stack Overflow

programmeradmin2浏览0评论

I want to remove   from the below code.

<div class="country-IN">
   <span class="locality">Barnala</span>
   &nbsp;&nbsp;
   <span class="state">Punjab</span>
   &nbsp;&nbsp;
   <span class="country">India</span>
</div>

Please help me to get out of it.

I want to remove &nbsp; from the below code.

<div class="country-IN">
   <span class="locality">Barnala</span>
   &nbsp;&nbsp;
   <span class="state">Punjab</span>
   &nbsp;&nbsp;
   <span class="country">India</span>
</div>

Please help me to get out of it.

Share Improve this question edited Jul 22, 2020 at 21:45 doğukan 27.4k13 gold badges62 silver badges75 bronze badges asked Mar 11, 2013 at 18:14 Manjit SinghManjit Singh 8744 gold badges9 silver badges12 bronze badges 2
  • remove it how? Can't you just open the file and hit delete? – Ryan B Commented Mar 11, 2013 at 18:16
  • This may be a code snippet stored in a CMS... – Marc Audet Commented Mar 11, 2013 at 18:17
Add a comment  | 

6 Answers 6

Reset to default 18

I'd suggest:

var el = document.querySelector('.country-IN');
el.innerHTML = el.innerHTML.replace(/&nbsp;/g,'');

JS Fiddle demo.

Or, with jQuery:

$('.country-IN').html(function(i,h){
    return h.replace(/&nbsp;/g,'');
});

JS Fiddle demo.

Or even:

$('.country-IN').children().each(function(i,e){
    this.parentNode.removeChild(this.nextSibling);
});

JS Fiddle demo.

Though it'd be easier to simply edit the HTML files themselves and just remove those strings of characters.

You can get the elements you want and add it back as the html for the element

$('.country-IN').html(function(i,v){
   return $('<div>').append($(this).children('*')).html(); 
});

FIDDLE

Library agnostic:

(function() {
var country_els = document.querySelectorAll('.country-IN');

for (var i = 0; i < country_els.length; i++) {
    for (var j = 0; j < country_els[i].childNodes.length; j++) {
        var node = country_els[i].childNodes[j];
        if (typeof node.tagName === 'undefined') {
            node.parentNode.removeChild(node);
        }
    }  
}
})();

Fiddle

First, get your whole HTML div and convert it to the string

convertHtmlToText(str)
{
   str = str.toString();
  return str.replace(/<[^>]*(>|$)|&nbsp;|&zwnj;|&raquo;|&laquo;|&gt;/g, ' ');
}

you will get the text without HTML tag and &nbsp etc

you can add multiple conditions in the above solution

We don't need to javascript for that. We can hide the &nbsp; with only CSS. Add font-size: 0 to container and font-size: 1rem to child.

.second {
  font-size: 0;
}

.second > span {
  font-size: 1rem;
}
<div class="country-IN">
  <span class="locality">Barnala</span>
  &nbsp;&nbsp;
  <span class="state">Punjab</span>
  &nbsp;&nbsp;
  <span class="country">India</span>
</div>

<div class="country-IN second">
  <span class="locality">Barnala</span>
  &nbsp;&nbsp;
  <span class="state">Punjab</span>
  &nbsp;&nbsp;
  <span class="country">India</span>
</div>

You can do this with preg_replace in PHP.

<?php
$html = "YOUR HTML"
$new = preg_replace("!&nbsp;!", "", $html);
print($new);
?>

I used this script before and it should work fine.

发布评论

评论列表(0)

  1. 暂无评论