删除  来自HTML

Man*_*ngh 1 html css jquery

我想 从下面的代码中删除.

<div class="country-IN">
   <span class="locality">Barnala</span>
   &nbsp;&nbsp;
   <span class="state">Punjab</span>
   &nbsp;&nbsp;
   <span class="country">India</span>
</div>
Run Code Online (Sandbox Code Playgroud)

请帮我摆脱它.

Dav*_*mas 14

我建议:

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

JS小提琴演示.

或者,使用jQuery:

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

JS小提琴演示.

甚至:

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

JS小提琴演示.

虽然简单地编辑HTML文件本身更容易,但只删除那些字符串.