gpt4 book ai didi

javascript - 为什么这个带有 javascript 的简单 .html 文件不起作用?

转载 作者:行者123 更新时间:2023-11-30 16:38:28 25 4
gpt4 key购买 nike

这段代码应该可以工作。为什么不呢?

<!DOCTYPE html>
<body>

<h1> The "^" operator in Javascript </h1>

<p id = "foo"></p>

<script>

function f(){
//vars
var a = 5;
var b = 13;
var c = a ^ b;
var binA = a.toString(2);
var binB = b.toString(2);
var binC = c.toString(2);
var strA = a.toString();
var strB = b.toString();
var strC = c.toString();

//make output string
var lineA = "a = " + strA + " = " binA + ", ";
var lineB = "b = " + strB + " = " binB + ", ";
var lineC = "c = " + strC + " = " binC + ", ";
var outputStr = lineA + lineB + lineC;

//output
document.getElementById("foo").innerHTML = outputStr;

}

f();

// For some reason this shows the contents of the header only

</script>
</body>

我对 a、b 和 c 进行了 toString() 转换,因为我不明白为什么输出无法在 Google Chrome 中显示。我承认他们不应该是必要的。但我想让代码尽可能的符合标准。我将字符串连接为子字符串,因为在此代码的前一次迭代中,注释掉字符串连接的“C”部分允许显示输出字符串的“A”和“B”相关部分。另外,它使视觉格式保持常规,我正试图用它来进行错误检查。

最佳答案

更新了代码,你漏掉了'+'

See this fiddle

var lineA = "a = " + strA + " = " + binA + ", ";
var lineB = "b = " + strB + " = " + binB + ", ";
var lineC = "c = " + strC + " = " + binC + ", ";
var outputStr = lineA + lineB + lineC;

关于javascript - 为什么这个带有 javascript 的简单 .html 文件不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com