Article Directory
First, write the location of JS
1, the label in the HTML body tag
<!--
可以将js代码编写到标签的onclick属性中;
当我们点击按钮时,js代码才会执行;
虽然可以写在标签的属性中,但是他们属于结构与行为耦合,不方便维护,不推荐使用。
-->
<button onclick="alert('讨厌,你点我干嘛~~');">点我一下</button>
<!--
可以将js代码写在超链接的href属性中,这样当点击超链接时,会执行js代码
-->
<a href="javascript:alert('让你点你就点!!');">你也点我一下</a>
<a href="javascript:;">你也点我一下</a>
2, within the script tag, script tags written in two places: in the head tag , if written in the head tag, recommendations written in the onload event of the window; in the body , if written in the body is generally recommended to write the end of the body label money.
<!--引入外部JS文件-->
<script type="text/javascript" src="js/script.js"></script>
<!--直接写在内部的-->
<script type="text/javascript">
alert("我是内部的JS代码");
</script>
Second, the comment about the JS
/*
这是一段多行注释
JS注释:被注释中的内容不会被执行,但是可以在源代码中查看
要养成良好的编写注释的习惯来说明自己的代码逻辑
*/
//这是一个单行注释
alert("hello");
document.write("hello");
console.log("hello"); //该语句用来在控制台输出一个日志
Third, the literals and variables
/*
* 字面量:都是一些不可改变的值
* 比如 :1 2 3 4 5
* 字面量都是可以直接使用,但是我们一般都不会直接使用字面量
*
* 变量:变量可以用来保存字面量(数据),而且变量的值是可以任意改变的
* 变量更加方便我们使用,所以在开发中都是通过变量去保存一个字面量,而很少直接使用字面量,可以通过变量对字面量进行描述。
*/
//声明变量:在js中使用var关键字来声明一个变量
var a; // 只是声明了一个变量,并没有进行赋值
//为变量赋值
a = 123;
a = 456;
a = 123124223423424;
//声明和赋值同时进行
var b = 789;
var c = 0;
var age = 80;
console.log(age);
//变量名支持中文,但是千万不要这么用
var 锄禾日当午 = 789;
console.log(锄禾日当午);
- Identifier (also can be said that the variable name)
- In JS and all that are by our self-named can be called is the identifier
- For example: variable names, function names, attribute names are all identifiers
- You need to comply with the following rules when naming an identifier:
- 1. The identifier may contain letters, numbers, _, $
- 2. Identifiers can not begin with a number
- 3. ES identifier can not be reserved words or keywords, categories such as: var, const, function ......
- 4. identifier generally used hump (hump large or a small hump) nomenclature
- Small hump: the first letter lowercase , beginning letter of each word capitalized, the rest lowercase letters
- Big Hump: the first letter capitalized , beginning letter of each word capitalized, the rest lowercase letters
- JS bottom during storage identifier Unicode encoding actually employed, so that in theory, all the contents contained in utf-8 can be used as an identifier.
- The first letter capitalized , beginning letter of each word capitalized, the rest lowercase letters
- JS bottom during storage identifier Unicode encoding actually employed, so that in theory, all the contents contained in utf-8 can be used as an identifier.