简体   繁体   English

Chrome中的jQuery html()编码错误

[英]Jquery html() encoding error in chrome

I'm developing an Java Web Application, I used some jQuery and a REST web services that output an JSON object with a list of Javascript objects using AJAX. 我正在开发Java Web应用程序,我使用了一些jQuery和REST Web服务,这些服务使用AJAX输出带有JSON对象列表的JSON对象。 All is ok but when I try to fill a table created with Javascript using jQuery.html() to a valid div, all hell broke loose in Chrome, including this 一切正常,但是当我尝试将使用jQuery.html()使用Javascript创建的表填充到有效div时,Chrome崩溃了,包括

Error: INVALID_STATE_ERR: DOM Exception 11 错误:INVALID_STATE_ERR:DOM异常11

in the Javascript console. 在Javascript控制台中。

The problem is like this, try this in chrome Javascript console: 问题是这样的,请在chrome Javascript控制台中尝试以下操作:

$('#ValidadorWrapper').html("<div>AMOXICILIN 100 C&amp;psulapsula</div>"); 

But if we delete the ampersand, it works 但是,如果我们删除“&”号,它将起作用

$('#ValidadorWrapper').html("<div>AMOXICILIN 100 Camp;psulapsula</div>"); 

This problem happens only in Chrome, I suspect it has something to do with the encoding characters but I cant' find any way of doing it. 此问题仅在Chrome中发生,我怀疑它与编码字符有关,但我找不到任何解决方法。 Obviously I need to input an & ( i mean the &amp; entity) in this document. 显然,我需要在此文档中输入& ( i mean the &amp; entity)

Some steps I have tried and didn't work: 我尝试过但不起作用的一些步骤:

  1. I'm using the gson library to output a String to an JSP page. 我正在使用gson库将字符串输出到JSP页面。 My JSP page have this header <%@page pageEncoding= "UTF-8" contentType="text/html; charset=UTF-8" %> . 我的JSP页面具有此标头<%@page pageEncoding= "UTF-8" contentType="text/html; charset=UTF-8" %> This was my first attempt and didn't work when an ampersand appeared in my JSON object (well any special char). 这是我的第一次尝试,当在我的JSON对象中出现“与”号(以及任何特殊字符)时,此方法不起作用。

  2. My second attempt was using the HTMLEntities Java library to encode all special chars. 我的第二次尝试是使用HTMLEntities Java库对所有特殊字符进行编码。 This is the actual version and it still doesn't work 这是实际版本,仍然无法使用

  3. Using unicode chars like \& doesnt work either 使用\&这样的Unicode字符也不起作用

There is something more strange. 还有更奇怪的东西。 Apparently if I use $('#ValidadorWrapper').html("AMOXICILIN 100 \&"); 显然,如果我使用$('#ValidadorWrapper').html("AMOXICILIN 100 \&"); it works!, but this is just an example. 可以!但是这只是一个例子。 I'm trying to fill an HTML table with my object so I really need to put that data inside html (table) tags 我正在尝试用我的对象填充HTML表,因此我确实需要将该数据放入html(表)标签中

尝试这个:

$('<div></div>').appendTo('#ValidadorWrapper').text('AMOXICILIN 100 C&aacute;psulapsula');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM