8-JavaScript 语句
body { margin: 0 }
#content-info { width: auto; margin: 0 auto; text-align: center }
#author-info { white-space: nowrap; text-overflow: ellipsis; overflow: hidden }
#title { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; padding-top: 10px; margin-bottom: 2px; font-size: 34px; color: rgba(80, 80, 80, 1) }
.text { white-space: nowrap; text-overflow: ellipsis; display: inline-block; margin-right: 20px; margin-bottom: 2px; font-size: 20px; color: rgba(140, 140, 140, 1) }
#navBar { width: auto; height: auto; position: fixed; right: 0; bottom: 0; background-color: rgba(240, 243, 244, 1); overflow-y: auto; text-align: center }
#svg-container { width: 100%; overflow-x: scroll; min-width: 0; margin: 0 10px; overflow: visible; position: relative }
#nav-thumbs { overflow-y: scroll; padding: 0 5px }
.nav-thumb { position: relative; margin: 10px auto }
.nav-thumb>p { text-align: center; font-size: 12px; margin: 4px 0 0 }
.nav-thumb>div { position: relative; display: inline-block; border: 1px solid rgba(198, 207, 213, 1) }
.nav-thumb img { display: block }
#main-content { bottom: 0; left: 0; right: 0; background-color: rgba(208, 207, 216, 1); display: flex; height: auto; flex-flow: row wrap; text-align: center }
#svg-container>svg { overflow: visible; display: block; margin: 5px auto }
#copyright { bottom: 0; left: 50%; margin: 5px auto; font-size: 16px; color: rgba(81, 81, 81, 1) }
#copyright>a { text-decoration: none; color: rgba(119, 119, 204, 1) }
.number { position: absolute; top: 0; left: 0; border-top: 22px solid rgba(8, 161, 239, 1); border-right: 22px solid rgba(0, 0, 0, 0) }
.pagenum { font-size: 12px; color: rgba(255, 255, 255, 1); position: absolute; top: -23px; left: 2px }
#navBar::-webkit-scrollbar { width: 8px; background-color: rgba(245, 245, 245, 1) }
#navBar::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 4px rgba(0,0,0,.3); border-radius: 8px; background-color: rgba(255, 255, 255, 1) }
#navBar::-webkit-scrollbar-thumb { border-radius: 8px; -webkit-box-shadow: inset 0 0 4px rgba(0,0,0,.3); background-color: rgba(107, 107, 112, 1) }
#navBar::-webkit-scrollbar-thumb:hover { background-color: rgba(74, 74, 79, 1) }
g[ed\:togtopicid],g[ed\:hyperlink],g[ed\:comment],g[ed\:note] {cursor:pointer;}
g[id] {-moz-user-select: none;-ms-user-select: none;user-select: none;}
svg text::selection,svg tspan::selection{background-color: #4285f4;color: #ffffff;fill: #ffffff;}
.st19 {fill:#303030;font-family:Arial;font-size:10pt}
.st18 {fill:#303030;font-family:Arial;font-size:14pt;font-weight:bold}
.st17 {fill:#ffffff;font-family:Arial;font-size:18pt;font-weight:bold}
JavaScript 语句
JavaScript 语句向浏览器发出的命令。语句的作用是告
诉浏览器该做什么。
JavaScript 语句
分号 ;
JavaScript 语句是发给浏览器的命令。
这些命令的作用是告诉浏览器要做的事情。
实例
JavaScript 语句向 id=”demo” 的 HTML 元素输出文本 “Hello Dolly”
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<h1>我的网页</h1>
<p id=”demo”>我的第一个段落。</p>
<script>
document.getElementById(“demo”).innerHTML = “你好 Dolly”;
</script>
</body>
</html>
分号用于分隔 JavaScript 语句。
通常我们在每条可执行的语句结尾添加分号。
使用分号的另一用处是在一行中编写多条语句。
实例
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<h1>我的网页</h1>
<p id=”demo1″></p>
<p id=”demo2″></p>
<script>
a = 1;
b = 2;
c = a + b;
document.getElementById(“demo1”).innerHTML = c;
x = 1; y = 2; z = x + y;
document.getElementById(“demo2″).innerHTML = z;
</script>
</body>
</html>
JavaScript 代码
JavaScript 代码是 JavaScript 语句的序列。
浏览器按照编写顺序依次执行每条语句。
本例向网页输出一个标题和两个段落:
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<h1>我的 Web 页面</h1>
<p id=”demo”>一个段落。</p>
<div id=”myDIV”>一个 DIV。</div>
<script>
document.getElementById(“demo”).innerHTML=”你好 Dolly”;
document.getElementById(“myDIV”).innerHTML=”你最近怎么样?”;
</script>
</body>
</html>
JavaScript 代码块
JavaScript 可以分批地组合起来。
代码块以左花括号开始,以右花括号结束。
代码块的作用是一并地执行语句序列。
本例向网页输出一个标题和两个段落:
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<h1>我的 Web 页面</h1>
<p id=”myPar”>我是一个段落。</p>
<div id=”myDiv”>我是一个div。</div>
<p>
<button type=”button” onclick=”myFunction()”>点击这里</button>
</p>
<script>
function myFunction()
{
document.getElementById(“myPar”).innerHTML=”你好世界”;
document.getElementById(“myDiv”).innerHTML=”你最近怎么样?”;
}
</script>
<p>当您点击上面的按钮时,两个元素会改变。</p>
</body>
</html>
JavaScript 语句标识符
JavaScript 语句通常以一个 语句标识符 为开始,并执行该语句。
语句标识符是保留关键字不能作为变量名使用。
下表列出了 JavaScript 语句标识符 (关键字) :
break 用于跳出循环。
catch 语句块,在 try 语句块执行出错时执行 catch 语句块。
continue 跳过循环中的一个迭代。
do … while 执行一个语句块,在条件语句为 true 时继续执行该语句块。
for 在条件语句为 true 时,可以将代码块执行指定的次数。
for … in 用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作)。
function 定义一个函数
if … else 用于基于不同的条件来执行不同的动作。
return 退出函数
switch 用于基于不同的条件来执行不同的动作。
throw 抛出(生成)错误 。
try 实现错误处理,与 catch 一同使用。
var 声明一个变量。
while 当条件语句为 true 时,执行语句块。
空格
JavaScript 会忽略多余的空格。
您可以向脚本添加空格,来提高其可读性。
下面的两行代码是等效的:
var person=”Hege”;
var person = “Hege”;
对代码行进行折行
您可以在文本字符串中使用反斜杠对代码行进行换行
正确地显示
错误的显示
document.write(“你好 \ W3Cschool!”);
document.write \ (“你好W3Cschool!”);