var input;
// method 1
input = document.getElementById('address').value;
alert(input)
// method 2
eval('input = "'+document.getElementById('address').value+'"')
alert(input)
方法 1 工作很好,但是方法 2 不工作当输入换行符 字符时,它显示“未终止的字符串文字”。
无论如何,我都需要使用 eval 来存储值。所以请帮助我。
最佳答案
使用 string.replace(/\r?\n/g, "\\n")
转义换行符。
关于javascript - 如何使用 javascript 转义 eval 中的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3830163/