01-JS 的使用方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 2. 内嵌式, 在 head 标签中,定义 script 标签, 在标签中书写 JS 代码 -->
<script>
alert('我是内嵌式 JS, 我执行了');
</script>
<!-- 3. 外链式, 在外部定义单独的 js 文件,书写 JS 代码,使用 script 标签的 src 属性引入 -->
<script src="./js/main.js"></script>
</head>
<body>
<!-- 1. 行内式, 给标签添加属性, 主要用于事件 事件="函数调用(参数)"
alert 是系统中自带的函数,作用是 弹窗
-->
<input type="button" value="点我呀,点我呀" onclick="alert('点我干嘛')">
</body>
</html>
04-代码/02-JS 中常见的数据类型.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var iAge = 10;
console.log(iAge, typeof(iAge));
var fHeight = 170.3;
console.log(fHeight, typeof(fHeight));
var sName = 'isaac';
console.log(sName, typeof(sName));
var bIsTrue = true;
console.log(bIsTrue, typeof(bIsTrue));
var oA;
console.log(oA, typeof(oA));
var oObject = null;
console.log(oObject, typeof(oObject));
</script>
</head>
<body>
</body>
</html>
03-JS 中的函数.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function fnHello(){
console.log('hello world!')
}
fnHello()
function fnSum(iNum1, iNum2){
var iNum = iNum1 + iNum2;
return iNum;
}
var result = fnSum(10, 20);
console.log(result);
</script>
</head>
<body>
</body>
</html>
04-局部变量和全局变量.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var iNum = 100;
function fnFunc(){
console.log('函数内部访问全局变量的值:', iNum);
iNum = 200;
var iAge = 10;
sName = 'isaac';
}
fnFunc()
console.log('函数外部访问修改后的全局变量的值:', iNum);
console.log('sName的值:', sName);
console.log('函数外部访问局部变量的值', iAge);
</script>
</head>
<body>
</body>
</html>
05-JS 装的判断语句.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var iAge = 17;
if (iAge >= 18){
console.log('可以进入网吧为所欲为,浏览各种网站');
}
if (iAge >= 18){
console.log('可以进入网吧为所欲为,浏览各种网站');
} else {
console.log('不满 18 岁,回去学习吧');
}
var fScore = 80;
if (fScore >= 90){
console.log("A");
} else if (fScore >= 80){
console.log("B");
} else if (fScore >= 60) {
console.log('C');
} else{
console.log("D")
}
</script>
</head>
<body>
</body>
</html>
06-JS 数组.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var aArray = new Array(1, 2, 'hello');
console.log(aArray, typeof(aArray));
var aList = [1, 2, 3.14, 'isaac', [3, 6, 9]];
console.log(aList, typeof(aList));
console.log('元素个数为:', aList.length);
console.log(aList[0], aList[3], aList[100]);
aList[3] = 'isaac NL';
console.log(aList);
aList[10] = 10;
console.log(aList, aList.length);
console.log(aList.indexOf(3.14), aList.indexOf(1000));
aList.push('last');
console.log(aList);
aList.pop()
console.log(aList);
aList.splice(2, 2);
console.log(aList);
aList.splice(2, 0, 3, 4, 5);
console.log(aList);
aList.splice(2, 2, 'hello', 'world');
console.log(aList);
</script>
</head>
<body>
</body>
</html>
07-JS 循环.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var i = 0;
while (i < 5){
console.log('写代码中....', i);
i++;
}
aList = ['郭德纲', '于谦', '小岳岳', '孙越']
for (var i=0; i < aList.length; i++) {
console.log(aList[i]);
}
do{
console.log('我至少执行一次');
} while(1 > 3);
</script>
</head>
<body>
</body>
</html>
08-JS 获取页面标签.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- <script>
var oDiv = document.getElementById('box');
alert(oDiv + " ---- " + typeof(oDiv));
</script> -->
<script>
window.onload = function(){
var oDiv = document.getElementById('box');
alert(oDiv + " ---- " + typeof(oDiv));
}
</script>
</head>
<body>
<div id="box">
这是一个 div
</div>
<!-- 解决方案一, 不建议 -->
<!-- <script>
var oDiv = document.getElementById('box');
alert(oDiv + " ---- " + typeof(oDiv));
</script> -->
</body>
</html>
09-JS 操作页面标签的属性.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function (){
var oInput = document.getElementById('name');
console.log('name属性:', oInput.name);
console.log('class属性:', oInput.className);
console.log('value 属性:', oInput.value);
console.log('style属性中的字体大小:', oInput.style.fontSize);
oInput.value = 100;
var oDiv = document.getElementById('div1');
console.log(oDiv.innerHTML);
console.log(oDiv.innerText);
oDiv.innerHTML = '好好学习';
}
</script>
</head>
<body>
<input type="text" name="name" class="box" id="name" value="10"
style="font-size: 26px;">
<div id="div1">
<p>
这是一个 div
</p>
</div>
</body>
</html>
10-定时器.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var oT = null;
function fnClick(){
oT = setInterval(alert, 2000, '好好学习')
}
function fnCalc(){
clearInterval(oT);
}
</script>
</head>
<body>
<div>
<p>点击单次定时器按钮, 3s后弹出 hello</p>
<!-- setTimeout(执行的函数名, 时间间隔, 第一个参数的参数) -->
<input type="button" value="单次定时器" onclick="setTimeout(alert, 3000, 'hello')">
</div>
<div>
<p>点击多次定时器按钮,间隔 2 秒, 弹出好好学习,点击 停止, 不再弹出</p>
<input type="button" value="多次定时器" onclick="fnClick()">
<input type="button" value="停止" onclick="fnCalc()">
</div>
</body>
</html>
11-jQuery 的使用.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 注意点: 引入外部 JS 的 script 和自己书写的 js 代码的标签,不能是同一个,需要单独定义 -->
<script src="js/jquery-1.12.4.min.js"></script>
<!-- 不能使用上述 script 标签书写 JS 代码, 需要重新定义 -->
<script>
window.onload = function() {
var oDiv = document.getElementById('box');
console.log('原生的 js:', oDiv);
}
$(document).ready(function (){
var $Div = $('#box');
console.log('jquery:', $Div);
});
$(function(){
var $Div = $('#box');
console.log('jquery 简写:', $Div);
});
</script>
</head>
<body>
<div id="box">
这是一个 div.
</div>
</body>
</html>
12-jQuery 选择器.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 引入 jQuery -->
<script src="js/jquery-1.12.4.min.js"></script>
<!-- 书写自己的 js 代码 -->
<script>
$(function(){
var result = $('div');
console.log(result.length);
console.log($('#box').length);
console.log($('.box').length);
console.log($('div p').length);
console.log($('div[class="box"]').length);
});
</script>
</head>
<body>
<div id="box" class="box">
这是第一个 div
<p>
好好学习
</p>
</div>
<div>
这是第二个 div
<p>
good good study
</p>
</div>
</body>
</html>
13-选择集转移.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-1.12.4.min.js"></script>
<script>
$(function(){
$('div').css({'background': 'red'});
$('div').has('p').css({'font-size': '28px'});
$('div').eq(1).css({'color': 'blue'});
});
</script>
</head>
<body>
<div>
这是第一个 div
<p>
好好学习
</p>
</div>
<div>
这是第二个 div <br><br>
good good study
</div>
</body>
</html>