![](/img/trans.png)
[英]Using replace to remove <strong> tags from html
[英].replace() not working to remove <strong> tags
您好我使用jquery獲取div的html內容,刪除強標簽,然后用新內容更新div。 但是由於某些原因它不起作用。 這是我的代碼:
var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
任何人都知道為什么這不起作用?
首先發出警報來檢查內容......
alert(content);
如果那樣有效,那么我就不會使用replaceWith,試試......
$('#mydivid').html(content);
第一:
你不需要替換兩次,replace()的第一個參數是正則表達式,所以你可以:
content.replace(/<\/?strong>/g, "");
刪除所有<strong>
和</strong>
標簽。
第二:
replaceWith()
不是你想要的, html()
是你的目標。
這就是你想要的:
$(function() {
$("#mydivid").html(function() {
return $(this).html().replace(/<\/?strong>/g, "");
});
});
jsfiddle: http : //jsfiddle.net/pS5Xp/
您可能不想使用replaceWith。 您應該再次調用.html()來更新包含新內容的div:
var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
我測試時工作正常 - http://jsbin.com/ihokav/edit#preview
一個更好的選擇是在強標記本身上使用replaceWith:
$('#mydivid strong').replaceWith(function() { return $(this).html(); });
我能用代碼看到的唯一問題是你正在替換div iteself。 你可能想要這樣做: -
var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
還要確保你有小寫的強標簽。
示例: http : //jsfiddle.net/8pVdw/
var content = $('#mydivid').html();
$('#mydivid').html(content.replace('<strong>', '').replace('</strong>', ''));
應該管用。
如果沒有,只需提醒內容並查看其是否有效。
你可以嘗試:
var div = $('#mydivid');
var strong = div.find("strong");
strong.replaceWith(strong.text());
這樣做只是找到<strong>
標簽並將其替換為文本內容。 這里有一個小提琴: http : //jsfiddle.net/cWpUK/1/
/**
* Syntax:
* Node replaceNode(Node newNode, Node oldNode)
* Node replaceNode(String newNode, Node oldNode)
* Node replaceNode(Object newNode, Node oldNode)
* - newNode: { String localName [, String namespaceURI ] }
* null replaceNode(null newNode, Node oldNode)
* - will delete the old node and move all childNodes to the parentNode
**/
// nodes from another document has to be imported first
// should throw an Error if newNode is descendant-or-self
// type of newNode is tested by 'firstChild' (DOM 1)
// returns new Node
var replaceNode = (function() {
var replaceNode = function(newNode, oldNode) {
var document = oldNode.ownerDocument;
if(newNode === null)
newNode = document.createDocumentFragment();
else if(typeof newNode === 'string' || newNode instanceof String)
newNode = {localName: newNode};
if(!('firstChild' in newNode)) {
var namespaceURI = 'namespaceURI' in newNode ?
newNode.namespaceURI : replaceNode.namespaceURI;
newNode = namespaceURI === null ?
document.createElement(newNode.localName) :
document.createElementNS(namespaceURI, newNode.localName);
}
var parentNode = oldNode.parentNode,
nextSibling = oldNode.nextSibling;
parentNode.removeChild(oldNode);
if(newNode.nodeType === 1 || newNode.nodeType === 11)
while(oldNode.firstChild)
newNode.appendChild(oldNode.firstChild);
parentNode.insertBefore(newNode, nextSibling);
return newNode.nodeType === 11 ? null : newNode;
};
replaceNode.namespaceURI = null;
return replaceNode;
})();
這將用另一個節點替換節點。 這樣做的好處是后續節點上沒有EventListener被銷毀。
var nodeList = document.getElementById('mydivid').getElementsByTagName('strong');
while(nodeList.length)
replaceNode(null, nodeList[ nodeList.length - 1 ]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.