[英]Observing unexpected Output : Javascript
a = Number(prompt()); b = Number(prompt()); if (a == b) { document.write("a==b") } else if (a > b) { document.write("a>b") } else { document.write("a<b") }
使用这个脚本 输入:a = 10, b = 20 Output: a
我期待 a<b 作为 output,如果我做错了什么,请帮忙。
<b
被解释为<b>
标记的开始。 您的(格式错误的 HTML)代码会导致浏览器呈现:
a
<b <="" body="">
</b>
不要使用document.write
- 如果您必须使用它,请记住您正在编写 HTML 标记,而不是纯文本。
Append 是文档的元素或文本节点。
const append = (text) => document.body.appendChild(document.createElement('span')).textContent = text; a = Number(prompt()); b = Number(prompt()); if (a == b) { append("a==b") } else if (a > b) { append("a>b") } else { append("a<b") }
您的结果中有“<”,这将被视为元素打开。 因此,您需要将 < 用于“<”,将 > 用于“>”。
a = Number(prompt()); b = Number(prompt()); if (a == b) { document.write("a==b") } else if (a > b) { document.write("a>b") } else { document.write("a<b") }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.