Jquery在属性中嵌入了引号
我有一个从数据库填充的自定义属性。 此属性可以包含这样的嵌入式单引号,
MYATT='Tony\'s Test'
在我的代码中的一些pont我使用jquery将此属性复制到这样的字段,
$('#MY_DESC').val($(recdata).attr('MYATT'));
MY_DESC是对话框中的文本字段。 当我显示对话框时,我在该字段中看到的是
托尼\
我需要看到的是,
托尼的考试
我怎么能解决这个问题所以我可以看到整个字符串?
尝试:
MYATT='Tony's Test'
我没有打扰使用HTML规范validation这一点,但维基百科条目说:
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.
如果您不使用双引号,请将您的自定义属性放入其中:)如果没有,我建议转义该值。
在设置文本字段的值之前,您可以尝试对字符串运行正则表达式以从字符串中删除所有反斜杠。
如果你这样做:
警报($(recdata).attr( ‘迈亚特’));
您将看到“Tony”的相同结果,这意味着浏览器未正确使用该值。 在这种情况下,转义的值不起作用。
您是否有能力在生成这些值时对其进行编辑? 您可以解析它们以在呈现之前包含转义值吗?