jQuery将文本标签转换为html标签 [英] jquery turn text tags to html tags

查看:733
本文介绍了jQuery将文本标签转换为html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var文本

我有一个填充div的字段,类似于在stackoverflow中询问问题的方式。 = $(#edit)。val();
$(#output)。text(text);

我希望能够使用选定的少数html标记,例如< b>大胆的例子。当我尝试这个时候,标签是以文本而不是html的形式出现的。如果可能的话,我希望能够搜索特定标签并将其转换为html。



任何人都可以帮忙吗?



感谢

解决方案

使用 .html()函数而不是 .text()函数,可以跳过除< b> 标签之外的所有内容。
$ b 编辑
该方法可能会因想要保留多少个标签以及需要编码多少而有所不同。

这将保持< b> 标签:

  var text ='这是< i>编码文字< / i>但是这是< b>粗体< / b> ;!'; 
var html = $('< div />')
.text(text)
.html()
.replace(new RegExp('& lt; (/)?b \\\s *& gt;','gi'),'< $ 1b>');


I have a field that fills in a div similar to how the asking a question works here on stackoverflow.

    var text = $("#edit").val();
    $("#output").text(text);

I want to be able to use a selected few html tags, like <b> bold for example. When I try this at the moment, the tags come up as text rather than html. If possible I want to be able to search for specific tags and make them into html.

Can anyone help?

thanks

解决方案

Use the .html() function instead of the .text() function, escape everything except the <b> tags.

Edit: The method could would vary based on how many tags you want to keep and how many to encode.
This will keep the <b> tag:

var text = 'This is <i>encoded text</i> but this is <b>bold</b >!';
var html = $('<div/>')
    .text(text)
    .html()
    .replace(new RegExp('&lt;(/)?b\\s*&gt;', 'gi'), '<$1b>');

这篇关于jQuery将文本标签转换为html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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