如何使用jQuery / JavaScript从h1标签中删除BOM?

时间:2017-12-14 11:54:10

标签: javascript jquery html

我如何使用jQuery或纯JavaScript来删除标签中的BOM?



<h1 id="test">Jo&#65279;h&#65279;n D&#65279;oe</h1>
&#13;
&#13;
&#13;

正如您所看到的,我在所有字母之间添加了BOM,并且它会在网站上显示正常情况,但是我试图直接修剪&#65279;的JavaScript。

我将如何执行此操作.text().html().innerHTML()将返回不包含BOM的普通字符串

1 个答案:

答案 0 :(得分:3)

您可以使用.encodeURI().decodeURI()

&#13;
&#13;
//Javascript by Id:
var div = document.getElementById('test');
var pomString = encodeURI(div.innerHTML);
pomString = pomString.replace(/%EF%BB%BF/g, '');
div.innerHTML = decodeURI(pomString);


//Javascript by Class:
var divClass = document.getElementsByClassName('test');
var pomStringClass = encodeURI(divClass[0].innerHTML);
pomStringClass = pomStringClass.replace(/%EF%BB%BF/g, '');
divClass[0].innerHTML = decodeURI(pomStringClass);


//JQuery by Id:
var divJquery = $('#jquery');
var pomJquery = encodeURI(divJquery.html());
pomJquery = pomJquery.replace(/%EF%BB%BF/g, '');
divJquery.html(decodeURI(pomJquery));


//JQuery by Class:
$('.jquery').each(function(){
  var pomJFilter = encodeURI($(this).html());
  pomJFilter = pomJFilter.replace(/%EF%BB%BF/g, '');
  $(this).html(decodeURI(pomJFilter));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Javascript by Id:

<h1 id="test">Jo&#65279;h&#65279;n D&#65279;oe</h1>

<hr>

Javascript by Class:
<h1 class="test">Jo&#65279;h&#65279;n D&#65279;oe</h1>
<h1 class="test">Jo&#65279;h&#65279;n D&#65279;oe</h1>

<hr>

JQuery by Id:
<h1 id="jquery">Jo&#65279;h&#65279;n D&#65279;oe</h1>

<hr>

JQuery by Class:
<h1 class="jquery">Jo&#65279;h&#65279;n D&#65279;oe</h1>
<h1 class="jquery">Jo&#65279;h&#65279;n D&#65279;oe</h1>
<h1 class="jquery">Jo&#65279;h&#65279;n D&#65279;oe</h1>
&#13;
&#13;
&#13;