[英]How to remove the BOM from a h1 tag with jQuery/JavaScript?
如何使用jQuery或僅使用普通JavaScript刪除標簽中的BOM?
<h1 id="test">John Doe</h1>
如您所見,我在所有字母之間添加了BOM表,它會正常顯示在網站上,但是我正試圖直接修剪
使用JavaScript。
我將如何執行此操作,因為.text()
.html()
.innerHTML()
都將返回不包含BOM的普通字符串
您可以使用.encodeURI()和.decodeURI() :
//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)); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Javascript by Id: <h1 id="test">John Doe</h1> <hr> Javascript by Class: <h1 class="test">John Doe</h1> <h1 class="test">John Doe</h1> <hr> JQuery by Id: <h1 id="jquery">John Doe</h1> <hr> JQuery by Class: <h1 class="jquery">John Doe</h1> <h1 class="jquery">John Doe</h1> <h1 class="jquery">John Doe</h1>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.