我做一個jQuery的的jQuery的insertBefore和單引號的問題
$.post('/blah', { blah : "some data" }, function(response) {
$(response).insertBefore($this); $this.val(''); }, "script"); }
On Rails的一面,create.erb.js
'<div style="background-color:yellow;"><%= show_content %></div>'
此代碼工作得很好,直到輸出insertBefore
包含'
它打破了HTML渲染。本質上,返回的字符串包含這樣的
'<div ...>It's a beautiful day</div>'
我on Rails的3.0.x和試圖在兩條軌道和Javascript並沒有什麼作品逃避轉義的各種組合,任何建議?我可以將'
更改爲"
,但這只是將問題轉移到另一個會破壞的角色。
編輯 好吧,這是方式更復雜,不過我想我發現了許多試驗和錯誤
On Rails的側面後的解決方案,create.erb.js
'<div style="background-color:yellow;"><%= raw(show_content.gsub(/'/, "%27")) %></div>'
jQuery的
$.post('/blah', { blah : "some data" }, function(response) {
$(unescape(response)).insertBefore($this); $this.val(''); }, "script"); }
你逃脫了只有一個符號,但有另一個符號,這裏不能保證你不會遇到它們 – megas
是的我測試了,但沒有其他特殊字符似乎有問題,只是單引號。 – Bob