Javas is a product language developed by the Internet police company, and has nothing to do with java.
Three ways to introduce JS in html page:
- Introduce js syntax format 1: write js code inside the script tag
<script type="text/javascript">
alert(1);
</script>`
- Introduce js syntax format 2: use external link src: import link file address
<script type="text/javascript" src="js/index.js"></script>
- Introduce js syntax format 3: js code can be nested in html code
<a href="#" onclick="alert(3)">点我啊</a>
Notes on using js:
- When using script tags, you must use double tags, not single tags
Not recommended:
`<a href="#" onclick="alert(3)">点我啊</a>`
不推荐的原因:
1.它会修改你当前url地址
2.它会跳转到当前页面的最顶部
推荐使用的语法格式:
<a href="javascript:void(0);" onclick="alert(3)">点我啊</a>
或者使用简写
<a href="javascript:;" onclick="alert(3)">点我啊</a> 这种方式是上面方式的简写
-->
Wording
<!--href="javascript:void(0);":我当前a标签不再超链接到一个新的界面,而是作为JS触发源而存在-->
variable
In js, variables are declared with var
. As long as it is a numeric type
in js, the number printed is number. In js, there is also Object type, and the Object class is the base class of all classes.
var obj = new Object();
console.debug(obj,typeof obj);//{} object
Note: Objects are displayed as {} in js
Operator
Basic operator: +-* / (When the string is operated in js-* /% will be converted to the corresponding number for operation)
console.debug(2+"3");//结果为5
而+,都是在做拼接
Strings and any data type + are stitching.
In JS, there are 6 false values: 0 null "" undefined NaN (not a number) false
Other values are all true
logical operator
&&: false is short circuit, When using js operation, either the first value is false or the last value
||: true is short circuit, when using js operation, the first value is true or the last value is
compared Operator
==: relatively equal, only compare values, not compare types
===: absolutely equal, not only to compare values, but also to compare types
Logical NOT operator
! The result value obtained is either true or false
//判断是否是一个数字的函数 isNaN 不是一个数字返回true,是一个数字返回false
console.debug(isNaN(e-f));
js declaration function
/*
* 在js中函数就相当于我们java中的方法
* 函数申明的语法格式:
* function 函数名([形参列表]){
* 函数体
* [return 返回结果值];
* }
*/
function show(){
console.debug("show show show show...");
}
//调用函数
show();
//申明函数,并且有参数
function showParam(param1,param2){
console.debug("show show show 参数1:"+param1+" 参数2:"+param2);
}
showParam("王天霸","张根锁");
//申明函数,并且返回结果值
function returnValue(){
return "李很弱";
}
//返回结果值,申明变量接收函数返回过来的 结果值
var value = returnValue();
console.debug("返回过来的结果值:"+value);
Anonymous function
/*什么叫做匿名函数:简单理解,没有名字的函数就叫做匿名函数*/
var fun = function(){
console.debug("show show show");
}
//注意:匿名函数需要一个变量名来接收
/*
var fun = function(){
console.debug("show show show");
}
就相当于
function fun(){
}
*/
fun();
Global variables and local variables Global variables and local variables
in js:
* Global variables: Variables declared outside the function or declared within the function without adding var are global variables
* Local variables: Variables declared inside the function and It is the
problem of overloading local variables that are declared with var
. Functions are not overloaded in js. When there are multiple functions with the same function name in js,
* it will only execute the last function, the previous functions are all
Note that * function calls will not be executed : The calling function has nothing to do with the function parameters. When you call the
* function, if no parameters are passed, the parameters of the receiver are displayed as undefined