如何使用jQuery解开文本? [英] How to unwrap text using jQuery?

查看:103
本文介绍了如何使用jQuery解开文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery打开HTML标签中的文本?



例如,如何转换这个HTML

 < p> A< i>句子< / i> < b>粗体字< / b>。< / p> 

放入(即移除粗体标记)

 < p> A< i>句子< / i>用粗体字。< / p> 

只使用jQuery而不使用正则表达式?

  $(b)。each(function (){
$(this).replaceWith(this.childNodes);
});

注意:这会保留HTML内容,其中 .text()可能会转换它。



如果您想完全删除< b>< / b> / code>您可以在jQuery 1.4 +中使用 Cheeso 的回答更轻松:

  $(p)。html(function(i,h){return h.replace(/< b> / g,'' ).replace(/< \ / b> / g,'');}); 


How to unwrap a text from a HTML tag using jQUery?

For instance, how to transform this HTML

<p>A <i>sentence</i> with <b>bold words</b>.</p>

into (i.e. remove the bold tags)

<p>A <i>sentence</i> with bold words.</p>

using only jQuery and no regex?

解决方案

You can do this:

  $("b").each(function() {
    $(this).replaceWith(this.childNodes);
  });

Note: this preserves whatever HTML you have inside where .text() might transform it.

If you wanted to quite literally just strip the <b></b> you can use Cheeso's answer a bit easier in jQuery 1.4+:

$("p").html(function(i,h){ return h.replace(/<b>/g,'').replace(/<\/b>/g,''); }); 

这篇关于如何使用jQuery解开文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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