繁体   English   中英

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

[英].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(); });

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

我能用代码看到的唯一问题是你正在替换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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM