.replace()无法删除标记

您好我使用jquery获取div的html内容,删除强标签,然后用新内容更新div。 但是由于某些原因它不起作用。 这是我的代码:

var content = $('#mydivid').html(); content = content.replace('', '').replace('', ''); $('#mydivid').html(content); 

任何人都知道为什么这不起作用?

首先发出警报来检查内容……

 alert(content); 

如果有效,那么我就不会使用replaceWith,试试……

 $('#mydivid').html(content); 

第一:

你不需要替换两次,replace()的第一个参数是正则表达式,所以你可以:

 content.replace(/<\/?strong>/g, ""); 

删除所有标签。

第二:

replaceWith()不是你想要的, html()是你的目标。

这就是你想要的:

 $(function() { $("#mydivid").html(function() { return $(this).html().replace(/<\/?strong>/g, ""); }); }); 

jsfiddle: http : //jsfiddle.net/pS5Xp/

我能用代码看到的唯一问题是你正在替换div iteself。 您可能想要这样做: –

 var content = $('#mydivid').html(); content = content.replace('', '').replace('', ''); $('#mydivid').html(content); 

还要确保你有小写的强标签。

示例: http : //jsfiddle.net/8pVdw/

 var content = $('#mydivid').html(); $('#mydivid').html(content.replace('', '').replace('', '')); 

应该管用。

如果没有,只需提醒内容并查看其是否有效。

你可以尝试:

 var div = $('#mydivid'); var strong = div.find("strong"); strong.replaceWith(strong.text()); 

这样做只是找到标签并将其替换为文本内容。 这里有一个小提琴: 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 ]); 

测试用例

您可能不想使用replaceWith。 您应该再次调用.html()来更新包含新内容的div:

 var content = $('#mydivid').html(); content = content.replace('', '').replace('', ''); $('#mydivid').html(content); 

我测试时工作正常 – http://jsbin.com/ihokav/edit#preview

一个更好的选择是在强标签本身上使用replaceWith:

 $('#mydivid strong').replaceWith(function() { return $(this).html(); }); 

http://jsbin.com/ihokav/2/edit