JS基础语法
1.基础语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基础语法</title>
</head>
<body>
</body>
<script type="text/javascript">
var a = 10;
var b = "123"
var c = '123'
var d = true;
var e = false;
var f = null;
var g;
console.log("5" == 5);
console.log("5" === 5);
console.log("5" != 5);
console.log("5" !== 5);
for(var a = 3; a < 10; a+=5){
console.log(a);
}
console.log(typeof a);
console.log(Number(true));
console.log(Number(false));
console.log(Number("123"));
console.log(Number("abc"));
console.log(Number("a1"));
console.log(Number("1a"));
console.log(Number(""));
console.log(Number(" "));
console.log(Number(undefined));
console.log(Number(null));
console.log(String(true))
console.log(Boolean(1231212));
console.log(Boolean(""));
console.log(3 + true);
console.log(null + 5);
console.log(false + "2");
console.log(5 - true);
console.log(7 + null);
console.log(6 + undefined);
console.log(parseInt(5.6));
console.log(parseInt("5.6"));
console.log(parseInt("34a6"));
console.log(parseInt("a1"));
console.log(parseInt(15,8));
var num = parseInt(Math.random() * (30 - 20 + 1) + 20);
console.log(num);
function test(a,b){
console.log(a + b)
return a * b;
}
var c = test(10,20,30);
console.log(c);
var fn = function (){
console.log("匿名函数")
};
(function (){
console.log("匿名函数1")
})();
(function (){
console.log("匿名函数2")
}());
fn();
console.log("hello word");
</script>
<script type="text/javascript" src="xx.js"></script>
</html>
简单的DOM操作
1.基础的DOM操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM</title>
<style type="text/css">
#first{
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div id="first"></div>
<input type="text" class="uName" />
<input type="button" value="点击" />
</body>
<script type="text/javascript">
var first = document.getElementById('first');
first.style.backgroundColor = "blue";
first.style.border = "5px gray solid";
first.style.borderRadius = "15px";
first.style.margin = "50px auto"
first.style.transform = "rotate(45deg)"
first.innerHTML = "<p>福<p>";
first.innerText = "<p>福<p>";
console.log(first.innerHTML);
var uNames = document.getElementsByClassName("uName");
uNames[0].value = "我是一个输入框"
console.log(uNames[0].value);
var button = document.getElementsByTagName("input");
var a = 1;
button[1].onclick = function(){
uNames[0].value = a++;
first.style.width = a * 10 + "px";
first.style.height = "100px";
first.style.transition = "1s";
console.log(uNames[0].value);
}
</script>
</html>
定时器
3.一次性定时器和循环定时器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>定时器</title>
<style>
#first{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 10px;
top: 10px;
}
</style>
</head>
<body>
<div id="first"></div>
</body>
<script type="text/javascript">
setTimeout(f1,1000);
function f1(){
console.log("执行了f1");
}
var timer = setInterval(function(){
console.log("循环定时器")
},2000);
setTimeout(function(){
clearInterval(timer);
},8000)
var first = document.getElementById('first');
first.onclick = function(){
var move = setInterval(function(){
first.style.left = first.offsetLeft + 5 + "px";
if (first.offsetLeft >= 500) {
clearInterval(move);
}
},20);
}
</script>
</html>