有没有一种简单的方法在JavaScript中将文本转换为HTML? [英] Is there an easy way to convert text into HTML in JavaScript?

查看:94
本文介绍了有没有一种简单的方法在JavaScript中将文本转换为HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用jQuery转义HTML字符串

JavaScript / jQuery HTML编码


例如,如果我想向用户显示字符串 x < 3 在HTML中我需要将< 字符替换为& lt; 。是否有一个预建的函数可以在JavaScript中执行此操作,或者也可以使用jQuery将任何文本字符串转换为相应的HTML?解析方案

想要使用jQuery,可以使用文本(字符串)方法。

  $(。selector)。text(x< 5); 

http://api.jquery.com/text/


Possible Duplicate:
Escaping HTML strings with jQuery
JavaScript/jQuery HTML Encoding

For example, if I wanted to show the user the string x < 3 in HTML I would need to replace the < character with &lt;. Is there a prebuilt function to do this in JavaScript or perhaps jQuery that converts any text string into the corresponding HTML?

解决方案

If you want to use jQuery, you can use the text(string) method.

$(".selector").text("x < 5");

http://api.jquery.com/text/

这篇关于有没有一种简单的方法在JavaScript中将文本转换为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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