.replacement()无法删除<strong>标签

.replace() not working to remove <strong> tags

本文关键字:lt gt 标签 strong 删除 replacement      更新时间:2023-09-26

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

var content = $('#mydivid').html();
content = content.replace('<strong>', '').replace('</strong>', '');
$('#mydivid').html(content);

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

首先放入警报以检查内容。。。

alert(content);

如果有效的话,我会def不使用replaceWith,试试。。。

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

第一个:

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

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

以移除所有的CCD_ 1和CCD_。

第二:

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('<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 ]);

测试用例

您可能不想使用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(); });

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