如何使用jQuery解码HTML实体?

本文翻译自:How to decode HTML entities using jQuery?

如何使用jQuery解码字符串中的HTML实体?


#1楼

参考:https://stackoom.com/question/4oTn/如何使用jQuery解码HTML实体


#2楼

The question is limited by 'with jQuery' but it might help some to know that the jQuery code given in the best answer here does the following underneath...this works with or without jQuery: 这个问题受到'with jQuery'的限制,但它可能有助于一些人知道在这里的最佳答案中给出的jQuery代码在下面做了以下...这有或没有jQuery:

function decodeEntities(input) {
  var y = document.createElement('textarea');
  y.innerHTML = input;
  return y.value;
}

#3楼

I think you're confusing the text and HTML methods. 我认为你混淆了文本和HTML方法。 Look at this example, if you use an element's inner HTML as text, you'll get decoded HTML tags (second button). 看看这个例子,如果你使用元素的内部HTML作为文本,你将得到解码的HTML标签(第二个按钮)。 But if you use them as HTML, you'll get the HTML formatted view (first button). 但是如果你将它们用作HTML,你将获得HTML格式的视图(第一个按钮)。

<div id="myDiv">
    here is a <b>HTML</b> content.
</div>
<br />
<input value="Write as HTML" type="button" onclick="javascript:$('#resultDiv').html($('#myDiv').html());" />
&nbsp;&nbsp;
<input value="Write as Text" type="button" onclick="javascript:$('#resultDiv').text($('#myDiv').html());" />
<br /><br />
<div id="resultDiv">
    Results here !
</div>

First button writes : here is a HTML content. 第一个按钮写道:这是一个HTML内容。

Second button writes : here is a <B>HTML</B> content. 第二个按钮写道:这是一个<B> HTML </ B>内容。

By the way, you can see a plug-in that I found in jQuery plugin - HTML decode and encode that encodes and decodes HTML strings. 顺便说一下,你可以看到我在jQuery插件中找到的插件 - HTML解码和编码 ,用于编码和解码HTML字符串。


#4楼

To decode HTML Entities with jQuery, just use this function: 要使用jQuery解码HTML实体,只需使用此功能:

function html_entity_decode(txt){
    var randomID = Math.floor((Math.random()*100000)+1);
    $('body').append('<div id="random'+randomID+'"></div>');
    $('#random'+randomID).html(txt);
    var entity_decoded = $('#random'+randomID).html();
    $('#random'+randomID).remove();
    return entity_decoded;
}

How to use: 如何使用:

Javascript: 使用Javascript:

var txtEncoded = "&aacute; &eacute; &iacute; &oacute; &uacute;";
$('#some-id').val(html_entity_decode(txtEncoded));

HTML: HTML:

<input id="some-id" type="text" />

#5楼

Like Mike Samuel said, don't use jQuery.html().text() to decode html entities as it's unsafe. 就像Mike Samuel所说,不要使用jQuery.html()。text()来解码html实体,因为它不安全。

Instead, use a template renderer like Mustache.js or decodeEntities from @VyvIT's comment. 相反,使用来自@ VyvIT评论的Mustache.jsdecodeEntities等模板渲染器。

Underscore.js utility-belt library comes with escape and unescape methods, but they are not safe for user input: Underscore.js实用程序带库附带了escapeunescape方法,但它们对用户输入不安全:

_.escape(string) _.escape(串)

_.unescape(string) _.unescape(串)


#6楼

Use 使用

myString = myString.replace( /\&amp;/g, '&' );

It is easiest to do it on the server side because apparently JavaScript has no native library for handling entities, nor did I find any near the top of search results for the various frameworks that extend JavaScript. 最简单的方法是在服务器端进行,因为很明显JavaScript没有用于处理实体的本机库,也没有找到任何接近扩展JavaScript的各种框架的搜索结果的顶部。

Search for "JavaScript HTML entities", and you might find a few libraries for just that purpose, but they'll probably all be built around the above logic - replace, entity by entity. 搜索“JavaScript HTML实体”,您可能会找到一些用于此目的的库,但它们可能都围绕上述逻辑构建 - 逐个实体替换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值