2009-08-11 35 views
1

我有一個正在從數據庫填充的自定義屬性。此屬性可以包含嵌入的單引號這樣,屬性中的jquery嵌入式引用

MYATT='Tony\'s Test' 

在一些蓬在我的代碼我使用jQuery這個屬性複製到這樣的領域,

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

MY_DESC是在文本字段一個對話框。當我顯示對話框所有我在現場看到的是

託尼\

我需要看到的是,

託尼的測試

我怎樣才能解決這個如此我可以看到整個字符串?

回答

2

嘗試:

MYATT='Tony&#x27s Test' 

我沒有理會與HTML規範驗證這一點,但wikipedia詞條中說:

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.

+0

這工作得很好..謝謝 – 2009-08-11 22:01:57

0

在設置文本字段的值之前,可以嘗試針對字符串運行正則表達式以從字符串中刪除所有反斜槓。

+0

我可以刪除反斜槓,我需要的是嵌入式報價。 – 2009-08-11 21:53:51

1

如果你不會使用雙引號,把你的自定義屬性進入他們:) 如果沒有,我建議逃避價值。

0

如果你這樣做:

警報($(recdata).attr( '邁亞特'));

您將看到「Tony \」的相同結果,即該值未被瀏覽器正確使用。在這種情況下逃脫的價值不起作用。

您是否有辦法編輯這些值?你能解析它們以在渲染之前包含轉義值嗎?