我试图在弹出窗口中显示两个单独结果的答案,但是我想在两个结果之间添加一些文本。这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var a;
function setValues1()
{
a = Number(document.getElementById("a").value);
}
function cube()
{
setValues1();
result1 = (a*a*a);
result2 = -1*(a*a*a);
alert("The volume of this cube is" +result1 & "and" +result2);
}
</script>
</head>
<body>
<input type="text" id="a" placeholder="Side Length..."/>
<input type="button" onclick="cube()" value="Calculate!"/>
</body>
</html>
如何修复它以显示两个结果之间的 和 ?
最佳答案
只需将 alert
语句中的 &
替换为 +
即可:
alert("The volume of this cube is " + result1 + " and " + result2);
不确定您使用 &
的意图是什么,但 +
用于 JavaScript 中的字符串连接。
关于javascript - 如何在 JavaScript 字符串中显示两个结果,中间有文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30471878/