使用 HTML 个实体设置元素值

Setting an element value using HTML entities

我在使用 HTML 实体时尝试设置输入字段的值时遇到问题,因为它们的字面意思是 " 而不是 "

这是我使用的代码:

document.getElementById('inputSurname').setAttribute('value', 'test"""');

其中输出为 test""" 虽然我希望输出为 test""".

它看起来不像是双重编码问题,因为在源代码中我看到它的方式与我在此处设置的方式相同。

我知道我可以从其 HTML 实体格式中解码值,尽管为了安全起见,我想避免这种情况。

任何帮助将不胜感激:)

试试这个:

document.getElementById('inputSurname').value = 'test"""';

或者如果您想保留 ":

function myFunction() {
    document.getElementById('myText').value = replaceQuot('test&quot&quot&quot');
}
function replaceQuot(string){
    return string.replace('&quot', '""');
}

好吧,您可以将新值写为 'test"""'

然而,对于其他角色,我将向您推荐这个答案:HTML Entity Decode

或者您可以使用转义字符。

document.getElementById("inputSurname").setAttribute("value", "test\"\"\"\"");