我写了一个功能:逃逸单引号在JavaScript
function makeTitleEditable($titleContainer){
var defaultValue = $titleContainer.text().replace("'","\\'");
$titleContainer.replaceWith("<input id='poll_title' name='poll[title]' value='" + defaultValue +"' type='text'>");
}
现在的问题是,我仍然不能逃脱单引号。例如,如果
$titleContainer.text() => I'm lucky
console.log("<input id='poll_title' name='poll[title]' value='" + defaultValue +"' type='text'>") => <input id='poll_title' name='poll[title]' value='I\'m lucky!' type='text'>
这会生成值为“I”而非“我很幸运”的DOM。我怎么解决这个问题?
为什么你不用你的属性使用双引号? – TimWolla