[英]Why does this simple .html file with javascript not work?
This code should work. 此代码应该起作用。 Why doesn't it?
为什么不呢?
<!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>
I did the toString() conversions on a, b and c because I can't understand why the output is failing to display in Google Chrome. 我对a,b和c进行了toString()转换,因为我不明白为什么输出无法在Google Chrome中显示。 I recognize they should not be necessary.
我认识到它们没有必要。 But I wanted to make the code as kosher as possible.
但是我想使代码尽可能的简洁。 I have concatenated the string as substrings because in a previous iteration of this code, commenting out the 'C' section of the string concatenation allowed the 'A' and 'B' related sections of the output string to display.
我将字符串串联为子字符串,因为在此代码的上一迭代中,注释掉字符串串联的'C'部分可以显示输出字符串的'A'和'B'相关部分。 Plus it keeps the visual format regular which I am trying to use to error-check.
另外,它使视觉格式保持规则,这是我尝试用于错误检查的格式。
Updated the code, you have missed out '+' 更新了代码,您错过了“ +”号
var lineA = "a = " + strA + " = " + binA + ", ";
var lineB = "b = " + strB + " = " + binB + ", ";
var lineC = "c = " + strC + " = " + binC + ", ";
var outputStr = lineA + lineB + lineC;
There is a syntax error, you missed '+' before the binA, binB and binC 语法错误,您在binA,binB和binC之前错过了'+'
var lineA = "a = " + strA + " = " + binA + ", ";
var lineB = "b = " + strB + " = " + binB + ", ";
var lineC = "c = " + strC + " = " + binC + ", ";
var outputStr = lineA + lineB + lineC;
output prints as below when I fixed that error in ff and chrome 当我修复ff和chrome中的错误时,输出如下所示
a = 5 = 101, b = 13 = 1101, c = 8 = 1000,
这是因为您在第24、25和26行的binA
, binB
和binC
之前缺少+
:)您有一些无效的javascript,并且在控制台中引发了错误。
You are making syntax error in javascript. 您在javascript中出现语法错误。 Replace your output string block with
将输出字符串块替换为
//make output string
var lineA = "a = " + strA + " = "+ binA + ", ";
var lineB = "b = " + strB + " = "+ binB + ", ";
var lineC = "c = " + strC + " = "+ binC + ", ";
var outputStr = lineA + lineB + lineC;
It will work for you. 它将为您工作。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.