admin管理员组

文章数量:1333450

Hi I am using jquery to get the html content of a div, remove the strong tags and then update the div with the new contents though. It isn't working for some reason though. Here is my code:

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

Anyone know why this isnt working?

Hi I am using jquery to get the html content of a div, remove the strong tags and then update the div with the new contents though. It isn't working for some reason though. Here is my code:

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

Anyone know why this isnt working?

Share Improve this question edited Oct 25, 2011 at 15:02 geoffs3310 asked Oct 25, 2011 at 14:56 geoffs3310geoffs3310 14k24 gold badges67 silver badges86 bronze badges 3
  • maybe you have < ____ strong > ???? – Royi Namir Commented Oct 25, 2011 at 14:58
  • 1 Works fine here: jsfiddle/mblase75/7RtAt -- does your code need to be inside a $(document).ready block? – Blazemonger Commented Oct 25, 2011 at 15:04
  • 4 Incidentally, you can replace both opening and closing tags at once using a regular expression: .replace(/<\/?strong>/g, '') – Blazemonger Commented Oct 25, 2011 at 15:07
Add a ment  | 

7 Answers 7

Reset to default 2

First put in an alert to check content...

alert(content);

if that works then I would def not use replaceWith, try...

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

first:

you don't need to replace twice, the first argument of replace() is regex, so you can :

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

to remove all the <strong> and </strong> label.

second:

replaceWith() is not what you want, html() is your target.

and this is all you want:

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

jsfiddle: http://jsfiddle/pS5Xp/

the only issue I can see with the code is that you are replacing the div iteself. You probably want to do this instead:-

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

Also make sure you have lower case strong tags.

Sample: http://jsfiddle/8pVdw/

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

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

Should work.

If it doesn't, just alert the contents and see if it works.

You could try:

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

What this does is just finds the <strong> tag and replaces it with the text contents. A fiddle here: http://jsfiddle/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;
})();

This will replace a Node with another one. This has the advantage that no EventListeners on subsequent Nodes are destroyed.

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

Testcase

You probably don't want to use replaceWith. You should update the div with the new content by calling .html() again:

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

Works fine when I test it - http://jsbin./ihokav/edit#preview

A nicer alternative is to use replaceWith on the strong tags themselves:

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

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

本文标签: javascriptreplace() not working to remove ltstronggt tagsStack Overflow