Javascript .replace()无法移除<;strong>;标签

Javascript .replace()无法移除<;strong>;标签,javascript,jquery,Javascript,Jquery,您好,我正在使用jquery获取div的html内容,删除强标记,然后用新内容更新div。但出于某种原因,它不起作用。这是我的密码: var content = $('#mydivid').html(); content = content.replace('<strong>', '').replace('</strong>', ''); $('#mydivid').html(content); var content=$('#mydivid').html(); 内容=

您好,我正在使用jquery获取div的html内容,删除强标记,然后用新内容更新div。但出于某种原因,它不起作用。这是我的密码:

var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
var content=$('#mydivid').html();
内容=内容。替换(“”,“”)。替换(“”,“”);
$('#mydivid').html(内容);

有人知道这为什么不起作用吗?

首先发出警报,检查内容

alert(content);
如果可以的话,我不会使用replaceWith,试试

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

您可能不想使用replaceWith。应通过再次调用.html()使用新内容更新div:


我能看到的代码的唯一问题是您正在替换div iteself。您可能希望这样做:-

var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);
var content=$('#mydivid').html();
内容=内容。替换(“”,“”)。替换(“”,“”);
$('#mydivid').html(内容);
还要确保有小写的强标记

示例:

var content=$('#mydivid').html();
$('#mydivid').html(content.replace(','').replace(','');
应该有用

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

您可以尝试:

var div = $('#mydivid');
var strong = div.find("strong");
strong.replaceWith(strong.text());
这样做只是找到
标记并用文本内容替换它。 这里有一把小提琴:

首先:

您不需要替换两次,replace()的第一个参数是regex,因此您可以:

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

这将用另一个节点替换一个节点。这样做的优点是不会破坏后续节点上的EventListener

var nodeList = document.getElementById('mydivid').getElementsByTagName('strong');
while(nodeList.length)
    replaceNode(null, nodeList[ nodeList.length - 1 ]);

也许您有
在这里工作很好:--您的代码是否需要在
$(文档)中。准备好了吗
块?顺便说一句,您可以使用正则表达式同时替换开始标记和结束标记:
。替换(//g',)
尝试过了,内容与html相同,强标记仍然存在,因此替换函数在某些情况下不起作用reason@geoffs3310:那么内容的确切价值是什么。。带有额外空格等。请选择代码,然后单击{}按钮来设置代码样式。使它更容易阅读:-)
var div = $('#mydivid');
var strong = div.find("strong");
strong.replaceWith(strong.text());
content.replace(/<\/?strong>/g, "");
$(function() {
    $("#mydivid").html(function() {
        return $(this).html().replace(/<\/?strong>/g, "");
    });
});
/**
 * 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;
})();
var nodeList = document.getElementById('mydivid').getElementsByTagName('strong');
while(nodeList.length)
    replaceNode(null, nodeList[ nodeList.length - 1 ]);