JavaScript控制流
<script>
let str = "icq123";
for (let index = 0; index
< str.length; index++) {
document.write(str[index]+"<br>");
}
</script>
let str = "icq123";
for (let index = 0; index
< str.length; ) {
document.write(str[index]+"<br>");
index++;
}
let sum = 0;
for (let index = 0; index <= 100; index++) {
sum += index;
}
document.write(sum);
let sum = 0;
for (let index = 1; index <= 100; index++) {
sum += 1/index;
}
document.write(sum);
for(let i in 数组/对象) { // i表示数组的下标/i表示键
数组名[i];
对象名[i];
}
let car = {name:"BYD", id:1234567890,color:"red"};
// in后面是对象或者数组
for (index in car) {
document.write(car[index]+"<br>");
}
let car = {name:"BYD", id:1234567890,color:"red"};
// in后面是对象或者数组
let txt = "";
for (index in car) {
txt += car[index] + "  ";
}
document.write(txt);
let array=["name", 123, true, "123.com"];
for (let index = 0; index < array.length; index++) {
document.write(array[index]+" ");
}
let sum = 0, i = 1;
while (i <= 100) {
sum += i;
i++;
}
document.write(sum);
let sum = 0, i = 1;
while (i <= 100) {
sum += 1/i;
i++;
}
document.write(sum);
let sum = 0, i = 1;
do {
sum = sum + i;
i++ ;
} while (i <= 100);
document.write(sum);
while(条件){语句体;}
只有满足条件的时才会执行语句体,如果第一次条件就不满足,那就意味着语句体一次也不执行
do{语句体;}while(条件)
先执行一次语句体,然后去判断条件满足情况,如果满足则继续执行。如果后续的条件不满足,该方式至少执行一次语句体。
break
:
continue:
for (let index = 0; index <= 5; index++) {
if (index == 3) {
break; // 跳出循环,并且后面的循环也不再执行
}
document.write(index+"<br>");
}
for (let index = 0; index <= 5; index++) {
if (index == 3) {
continue; // continue: 跳过本次循环,从下一次循环开始继续执行
}
document.write(index+"<br>");
}
时间
let time = new Date();
time.getDate();
time.getDay();
time.getTime();
数学
Math.random() //取0到1之间的随机数
Math.floor(Math.random()*100)
JavaScript DOM操作
<style>
#div1{
height: 100px;
width: 100px;
background-color: red;
}
</style>
</head>
<body>
<script>
"use strict";
document.getElementById('div1').onclick=alert('kali');
</script>
<div id="div1" onclick="alert('kali')">nezuko</div>
</body>
<input type="radio" name="gender" value="男">男<br>
<input type="radio" name="gender" value="女">女
<script>
function changeGender(val) {
// val表示某个标签
alert("选择的性别是"+ val.value);
}
</script>
</head>
<body>
<table border="1" style="width: 100px; height: 100px;">
<tr>
<td>
<input type="radio" name="gender" value="男" onclick="changeGender(this)">男<br>
<input type="radio" name="gender" value="女" onclick="changeGender(this)">女
</td>
</tr>
</table>
</body>
<head>
<script>
function changeGender(val) {
document.getElementById('text').value = val.value;
}
</script>
</head>
<body>
<table border="1" style="width: 100px; height: 100px;">
<tr>
<td>
<input type="radio" name="gender" value="男" onclick="changeGender(this)">男<br>
<input type="radio" name="gender" value="女" onclick="changeGender(this)">女<br>
</td>
</tr>
</table>
<!-- 行内标签,块标签 -->
<textarea name="" id="text" cols="30" rows="10"></textarea>
</body>
<head>
<script>
function checkPass(val) {
let password = val.value;
if (password.length < 6 ) {
alert("弱密码");
} else {
}
}
</script>
</head>
<body>
请输入密码:
<input type="text" name="pwd" id="pwd" style="width: 100px; height: 20px;" onblur="checkPass(this)" required="required">
</body>