这段代码不会在网页上输出任何内容吗?

时间:2019-04-30 01:08:53

标签: javascript

我想通过输出代码来完成对课堂的分配,但它不起作用。我尝试在整个代码中放置随机打印语句,以查看代码的哪一部分失败了,但仍然没有任何结果。

var results1;
var results2;
var results3;
var results4;
var results5;
var results6;
var results7;
var results8;

// Calculations
results1 = (7 > 5);
results2 = (5 < 5);
results3 = ("a" < "b"); 
results4 = (100 <= 99); 
results5 = ("100" !== 100); 
results6 = (0 > 10);
results7 = ("100" > "99"); 
results8 = ("A" == 92); 

// Output
document.write("1. (7 > 5)    --> " + results1 + "\n" );
document.write("2. (5 < 5)    --> " + results2 + "\n" );
document.write("3. ("a" < "b")    --> " + results3 + "\n" );
document.write("4. (100 <= 99)    --> " + results4 + "\n" );
document.write("5. ("100" !== 100)    --> " + results5 + "\n" );
document.write "6. (0 > 10)    --> " + results6 + "\n" );
document.write("7. ("100" > "99")    --> " + results7 + "\n" );
document.write("8. ("A" === 92)    --> " + results8 + "\n" );

2 个答案:

答案 0 :(得分:1)

这是因为您的引号没有正确转义。将您的输出部分更改为此:

document.write("1. (7 > 5)    --> " + results1 + "<br/>" );
document.write("2. (5 < 5)    --> " + results2 + "<br/>" );
document.write("3. (\"a\" < \"b\")    --> " + results3 + "<br/>" ); // Added "\" 
document.write("4. (100 <= 99)    --> " + results4 + "<br/>" );
document.write("5. (\"100\" !== 100)    --> " + results5 + "<br/>" ); // Added "\" 
document.write("6. (0 > 10)    --> " + results6 + "<br/>" ); // Added "(" 
document.write("7. (\"100\" > \"99\")    --> " + results7 + "<br/>" ); // Added "\" 
document.write("8. (\"A\" === 92)    --> " + results8 + "<br/>" ); // Added "\" 

答案 1 :(得分:1)

使用单引号使您不必转义双引号,但也应该使用html <br/>而不是"\n"

换行
document.write('1. (7 > 5)    --> ' + results1 + "<br/>" );
document.write('2. (5 < 5)    --> ' + results2 + "<br/>" );
document.write('3. ("1" < "b")    --> ' + results3 + "<br/>" );
document.write('4. (100 <= 99)    --> ' + results4 + "<br/>" );
document.write('5. ("100" !== 100)    --> ' + results5 + "<br/>" );
document.write('6. (0 > 10)    --> ' + results6 + "<br/>" );
document.write('7. ("100" > "99")    --> ' + results7 + "<br/>" );
document.write('8. ("A" === 92)    --> ' + results8 + "<br/>" );

您还可以使用https://jsfiddle.net/,它会向您显示语法问题。 https://jsfiddle.net/