简体   繁体   English

使用编码的HTML在Javascript中设置值

[英]Setting Value in Javascript with encoded HTML

Quick question, I have a simple javascript problem that I don't know the fix to. 快速问题,我有一个简单的JavaScript问题,我不知道该如何解决。

divNext = document.getElementById("submit_button");
if(document.getElementById("page").value == 4 ? divNext.value = 'Submit' : divNext.value = 'Next »');

Now the problem is that it should display the Next with two >> after it (at least if they where encoded) however it displays them with the actual content there. 现在的问题是,它应该在其后显示带有两个>>的Next(至少,如果对它们进行了编码),但是它在其中显示了实际的内容。 How can I get it to display the html encoded character? 如何获得显示html编码字符的信息?

That is because you have HTML-encoded the character instead of encoding it to be in a Javascript string. 那是因为您已经对该字符进行了HTML编码,而不是将其编码为Javascript字符串。

And by the way, you are using both the if and the ? 顺便说一句,您同时使用了if? conditional operator incorrectly. 条件运算符错误。

divNext = document.getElementById("submit_button");
divNext.value = document.getElementById("page").value == 4 ? 'Submit' : 'Next \xBB';

Or using if : 或使用if

divNext = document.getElementById("submit_button");
if (document.getElementById("page").value == 4) {
  divNext.value = 'Submit'
} else {
  divNext.value = 'Next \xBB';
}

Just put them in 放进去

document.getElementById("page").value == 4 ? divNext.value = 'Submit' : divNext.value = 'Next >>');

or use .innerHTML 或使用.innerHTML

document.getElementById("page").value == 4 ? divNext.innerHTML = 'Submit' : divNext.innerHTML = 'Next »»');

Also, if you are using the ternary operator why do you have an if ? 另外,如果您使用的是三元运算符,为什么会有if呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM