jQuery嵌入属性中的报价 [英] Jquery embedded quote in attribute

查看:57
本文介绍了jQuery嵌入属性中的报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义属性,该属性正在从数据库中填充.此属性可以包含这样的嵌入式单引号,

I have a custom attribute that is being filled from a database. This attribute can contain an embedded single quote like this,

  MYATT='Tony\'s Test'

在我的代码中,我使用jquery将此属性复制到这样的字段中,

At some pont in my code I use jquery to copy this attribute to a field like this,

 $('#MY_DESC').val($(recdata).attr('MYATT'));

MY_DESC是对话框中的文本字段.当我显示对话框时,我在该字段中看到的全部是

MY_DESC is a text field in a dialog box. When I display the dialog box all I see in the field is

托尼\

我需要看的是

托尼测验

如何解决此问题,以便可以看到整个字符串?

How can I fix this so I can see the entire string?

推荐答案

尝试:

MYATT='Tony&#x27s Test'

我没有费心用HTML规范进行验证,但是维基百科条目说:

I didn't bother verifying this with the HTML spec, but the wikipedia entry says:

The ability to "escape" characters in this way allows for the characters < and & (when written as &lt; and &amp;, respectively) to be interpreted as character data, rather than markup. For example, a literal < normally indicates the start of a tag, and & normally indicates the start of a character entity reference or numeric character reference; writing it as &amp; or &#x26; or &#38; allows & to be included in the content of elements or the values of attributes. The double-quote character ("), when used to quote an attribute value, must also be escaped as &quot; or &#x22; or &#34; when it appears within the attribute value itself. The single-quote character ('), when used to quote an attribute value, must also be escaped as &#x27; or &#39; (should NOT be escaped as &apos; except in XHTML documents) when it appears within the attribute value itself. However, since document authors often overlook the need to escape these characters, browsers tend to be very forgiving, treating them as markup only when subsequent text appears to confirm that intent.

这篇关于jQuery嵌入属性中的报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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