- html>
- <html>
- <head>
- <meta charset = "UTF-8"/>
- <title>title>
- head>
- <body>
- <p>JavaScript 空白字符p>
- <h3>HTMLh3>
- <p id = "demo1">p>
- <script>
- var person = "HTML"
- var h,g,y;
- var h = 3; g = 9;
- var y = h * g;
- document.getElementById("demo1").innerHTML = y;
- script>
- body>
- html>
JavaScript 空白字符
JavaScript 会忽略多个空格。您可以向脚本添加空格,以增强可读性
- html>
- <html>
- <head>
- <meta charset = "UTF-8/">
- <title>title>
- head>
- <body>
- <p>JavaScript 行长度和折行p>
- <h3>htmlh3>
- <p id = "demo1">p>
- <script>
- document.getElementById("demo1").innerHTML = "HTML CSS "
- script>
- body>
- html>
JavaScript 行长度和折行
为了达到最佳的可读性,程序员们常常喜欢把代码行控制在 80 个字符以内。
如果 JavaScript 语句太长,对其进行折行的最佳位置是某个运算符
- html>
- <html>
- <head>
- <meta charset = "UTF-8"/>
- <title>title>
- head>
- <body>
- <p>JavaScript 代码块p>
- <h3>cssh3>
- <button type = "button" onclick = myFunction >HTMLbutton>
- <p id = "demo1">p>
- <p id = "demo2">p>
- <script>
- function myFunction(){
- document.getElementById("demo1").innerHTML = "html";
- document.getElementById("myDIV").innerHTML = "css";
- }
- script>
- body>
- html>
JavaScript 代码块
JavaScript 语句可以用花括号({...})组合在代码块中。
代码块的作用是定义一同执行的语句
- html>
- <html>
- <head>
- <meta charset = "UTF-8"/>
- <title>title>
- head>
- <body>
- <p>JavaScript 字面量p>
- <h3>HTMLh3>
- <p id = "demo1">p>
- <script>
- document.getElementById("demo1").innerHTML = 30.09;
- script>
- body>
- html>
JavaScript 字面量
书写混合值最重要的规则是
写数值有无小数点均可
- html>
- <html>
- <head>
- <meta charset = "UTF-8"/>
- <title>title>
- head>
- <body>
- <p>JavaScript 字符串p>
- <h3>HTMLh3>
- <p id = "demo">p>
- <p id = "demo1">p>
- <script>
- document.getElementById("demo").innerHTML = "HTML CSS"
- docuemnt.getElementById("demo1").innerHTML = "html css"
- script>
- body>
- html>
JavaScript 字符串
字符串是文本,由双引号或单引号包围
- html>
- <html>
- <head>
- <meta charset = "UTF-8"/>
- <title>title>
- head>
- <body>
- <p>JavaScript 变量p>
- <h3>HTMLh3>
- <p id = "demo1">p>
- <script>
- var x;
- x = 9;
- var h;
- h = 19;
- document.getElementById("demo").innerHTML = x;
- document.getElementById("demo1").innerHTML = h;
- script>
- body>
- html>
JavaScript 变量
在编程语言中,变量用于存储数据值。
JavaScript 使用 var 关键词来声明变量。
= 号用于为变量赋值。