.replace()无法删除< strong>标签 [英] .replace() not working to remove <strong> tags

查看:280
本文介绍了.replace()无法删除< strong>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery获取div的html内容,删除强标记,然后使用新内容更新div.由于某种原因,它无法正常工作.这是我的代码:

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?

推荐答案

第一:

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

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

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

删除所有<strong></strong>标签.

秒:

replaceWith()不是您想要的,html()是您的目标.

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

这就是您想要的:

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

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

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

这篇关于.replace()无法删除&lt; strong&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆