简介:
1、最流行脚本语言,简称 JS
2、编写在 HTML 页面中,由浏览器负责解释和执行
3、增加html页面的交互能力
语法应用:
1、内嵌式:
<html>
<head>
<meta charset="UTF-8">
<title>JS</title>
</head>
<body>
<button onclick="alert('你好 JS');">Hello JS</button>
</body>
</html>
2、内部式:
<html>
<head>
<meta charset="UTF-8">
<title>JS</title>
<script type="text/javascript">
alert(“你好 JS”);
</script>
</head>
<body>
</body>
</html>
3、 外部式:
<html>
<head>
<meta charset="UTF-8">
<title>JS</title>
</head>
<body>
<script src="main.js">
</script>
</body>
</html>
变量:
1、变量名只能包含:字母、数字、下划线( _ )
2、不能以数字开头
3、不能使用JS关键字
4、变量名区分大小写
注释:
单行注释: // 注释语句 快捷键ctrl+/
多行注释: /* 注释语句 */ 快捷键ctrl+shift+/
注意:多行注释相互不能嵌套使用,只能在多行注释里面使用单行注释!
函数function:
1、无参函数:
function helloJS( )
{
alert("你好 JS");
}
2、带参函数:
function helloJS(name,age)
{
alert("你好 " + name + age);
}
3、带有返回值的函数:
function add( num1, num2 )
{
return num1 + num2;
}
4、 内置系统函数:
alert()//显示带有一段消息和一个确认按钮的警告框
prompt()//显示可提示用户输入的对话框
isNaN(value)//用于检查其参数是否是非数字值
数据类型:
数值型:number(凡是数字都是数值型,不区分整数和小数)
字符串:string(凡是引号包裹起来的内容全部都是字符串)
布尔:boolean(true、false)
对象类型:object(特殊取值null)
未定义型:undefined
浏览器对象模型:
1、BOM对象:
Window 浏览器窗口(BOM的根对象)
Window Document 处理页面文档
Window Screen 获取用户屏幕信息
Window Location 获取页面地址信息/页面定向
Window History 页面历史控制
Window Navigator 获取浏览器相关信息(浏览器版本)
2、window对象:
<script type="text/javascript">
var winWidth = window.innerWidth; //获取浏览器内部宽度
var winWidth = innerWidth; //可以省略 window.
Document.write(winWidth );
var winHeight = window.innerHeight; //获取浏览器内部高度
window.open(‘http://www.baidu.com’); //打开一个新窗口(必须指定http或https)
window.close(); // 关闭当前窗口
</script>
3、 Document 对象:
<script type="text/javascript">
var name = document.getElementById('name'); // 获取指定id的HTML元素
document.write(‘Hello’); // 在页面输出内容
var title = document.title; // 获取文档标题
var cookies = document.cookie; // 获取Cookie
document.location = "xxx.html"; // 当前页面重定向到 xxx.html 页面
</script>
4、History 对象:
<script type="text/javascript">
history.back(); // 返回上一页面
history.forward(); // 返回下一页面
history.go(i); // i 为数字,返回到指定页面
i=-1 表示上一页,等同于 history.back();
i=1 表示下一页,等同于 history.forward();
</script>