我有一个从数据库填充的自定义属性。此属性可以包含嵌入式单引号,
MYATT='Tony\'s Test'
在我的代码中,我使用jquery将此属性复制到这样的字段
$('#MY_DESC').val($(recdata).attr('MYATT'));
MY_DESC是对话框中的文本字段。当我显示对话框时,我在字段中看到的是
托尼\
我需要看到的是,
Tony的考试
如何解决此问题,以便查看整个字符串?
答案 0 :(得分:2)
尝试:
MYATT='Tony's Test'
我没有打扰使用HTML规范验证这一点,但wikipedia条目说:
The ability to "escape" characters in this way allows for the characters < and & (when written as < and &, 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 & or & or & 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 " or " or " when it appears within the attribute value itself. The single-quote character ('), when used to quote an attribute value, must also be escaped as ' or ' (should NOT be escaped as ' 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.
答案 1 :(得分:1)
如果您不使用双引号,请将自定义属性放入其中:) 如果没有,我建议逃避价值。
答案 2 :(得分:0)
在设置文本字段的值之前,您可以尝试对字符串运行正则表达式以从字符串中删除所有反斜杠。
答案 3 :(得分:0)
如果你这样做:
警报($(recdata).attr( '迈亚特'));
您将看到“Tony”的相同结果,这意味着浏览器未正确使用该值。在这种情况下,转义的值不起作用。
您是否有能力在生成这些值时对其进行编辑?你可以解析它们以在呈现之前包含转义值吗?