01 奇葩for循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>奇葩for循环</title>
</head>
<body>
</body>
</html>
<script>
// 正常for循环写法
for(let i=0;i<10;i++){
console.log(i);
}
// 没有初始值
let k=0;
for(;k<10;k++){
console.log(k);
}
// 没有初始值和判断是否满足循环条件
// 如果for循环中没有判断条件,那么它的判断条件在循环体内部,并且需要开发者手动书写跳出循环的操作,因为不会主动跳出循环
let m=0;
for(;;m++){
if(m<10){
console.log(m);
}else{
break;
}
}
// 没有初始值,没有判断条件,没有递增
let =0;
for(;;){
if(n<10){
console.log(n);
n++;//递增
}else{
break;
}
}
</script>
02 文档流写入和元素写入
把元素直接写入文档流 使用document.write(str)
1.会把内容渲染到body
2.str 可以是标签,也可以是文本内容
3.后面写入的内容不会覆盖前面写入的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文档流写入和元素写入</title>
<style>
*{
margin: 0;
padding: 0;
}
.cell{
width: 50px;
height: 50px;
background: #04be02;
margin: 10px;
}
.wp{
width: 200px;
height: 200px;
background: #f60;
}
</style>
</head>
<body>
<div class="wp" id="aa"></div>
<div class="wp" id="bb">
<h1>中午吃啥</h1>
<h2>米饭</h2>
<h2>面条</h2>
</div>
</body>
</html>
<script>
/*
把元素直接写入文档流 使用document.write(str)
1.会把内容渲染到body
2.str 可以是标签,也可以是文本内容
3.后面写入的内容不会覆盖前面写入的内容
*/
document.write('<h1>秦琼</h1>');
document.write('<h1>秦用</h1>');
document.write('<h1>秦桧</h1>');
document.write('<div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div>')
</script>
innerHTML
它是dom元素的一个属性,可以通过 innerHTML 设置和获取元素内容
特点:
1.后写入的内容会覆盖之前写入的内容
2.可以写入标签,也可以写入文本
设置内容
dom.innerHTML = '内容' 如果内容是html结构,可以直接被渲染称html
获取内容:
dom.innerHTML 获取到了dom元素中的html结构
innerText
它是dom元素的一个属性,可以通过 innerText 设置和获取元素的文本内容
特点:
1.后面写入的内容会覆盖前面写入的内容
2.设置的时候如果是html结构,html结构不会被渲染,会原样写入
和innerHTML 一样也可以设置和获取内容
注意,获取的结果 会 过滤到标签名,只会显示文本内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文档流写入和元素写入</title>
<style>
*{
margin: 0;
padding: 0;
}
.cell{
width: 50px;
height: 50px;
background: #04be02;
margin: 10px;
}
.wp{
width: 200px;
height: 200px;
background: #f60;
}
</style>
</head>
<body>
<div class="wp" id="aa"></div>
<div class="wp" id="bb">
<h1>中午吃啥</h1>
<h2>米饭</h2>
<h2>面条</h2>
</div>
</body>
</html>
<script>
let aa = document.getElementById('aa');
aa.innerHTML = '铁木真';
aa.innerHTML += '术赤'; //aa.innerHTML = aa.innerHTML+'术赤'
aa.innerHTML += '<h1>察合台</h1>';
console.log(aa.innerHTML);
let bb = document.getElementById('bb');
console.log(bb.innerHTML);
console.log(bb.innerText);
bb.innerText = '<h1>周末吃顿好的</h1>';
bb.innerText += '<h1>周末吃顿红烧大肠</h1>';
</script>
03 循环嵌套
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>循环嵌套</title>
<style>
*{
margin: 0;
padding: 0;
}
.wp{
width: 30px;
height: 30px;
border: 3px #f00 solid;
border-radius: 50%;
margin: 10px;
display: inline-block;
}
</style>
</head>
<body>
<!-- <div class="wp"></div> -->
</body>
</html>
<script>
/*
循环嵌套
*/
// 外层控制的是 行
for(let i=0;i<5;i++){
// 内层控制的是 列
for(let k=0;k<=i;k++){
document.write(k);
}
document.write('<br/>');
}
// 双重for循环绘制
for(let a=0;a<5;a++){
for(let b=0;b<5;b++){
document.write('<div class="wp"></div>');
}
document.write('<br/>');
}
//
for(let c=0;c<5;c++){
for(let d=0;d<=c;d++){
document.write('<div class="wp"></div>');
}
document.write('<br/>');
}
//
document.write('<div class="wp">');
document.write('<span>袁洪</span>');
document.write('</div>');
//
document.write('<table border=1 width="600">');
for(let i=0;i<4;i++){
document.write('<tr>');
for(let j=0;j<4;j++){
document.write('<td>'+j+'</td>');
}
document.write('</tr>');
}
document.write('</table>');
// 乘法表
document.write('<table border=1 width="600">');
for(let i=1;i<=9;i++){
document.write('<tr>');
for(let j=1;j<=9;j++){
if(j<=i){
document.write('<td>');
document.write(j+'x'+i+'='+(j*i));
document.write('</td>');
}else{
document.write('<td></td>')
}
}
document.write('</tr>');
}
document.write('</table>');
</script>
04 随机数
数学方法:
Math.floor() 向下取整
Math.ceil() 向上取整
Math.round() 四舍五入
Math.random() 随机数,返回值是[0,1)的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机数</title>
</head>
<body>
</body>
</html>
<script>
document.write(Math.random() + '<hr/>');
document.write(Math.random() * 100 + '<hr/>');
// 0~100整数
let num = Math.floor(Math.random() * 101);
document.write(num + '<hr/>');
// [40,60]之间的随机整数
let num1 = Math.floor(Math.random() * 21 + 40);
document.write(num1 + '<hr/>');
/*
[m,n]之间的随机整数
Math.floor(Math.random()*(n-m+1))+m;
*/
// 随机输出 10个[20,70]之间的整数
document.write('<br/>')
for (let i = 0; i <= 9; i++) {
let num2 = Math.floor(Math.random() * 51) + 20;
document.write(num2 + '<hr/>');
}
</script>
for循环中,如果判断条件具有多个,如下:具有i<6,j<20两个条件,那么只有最后一个条件起到了判断条件的作用,其他判断条件作废
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机数</title>
</head>
<body>
</body>
</html>
<script>
for (let i = 0, j = 0; i < 6, j < 20; i++, j++) {
sum = i + j;
}
document.write(sum + '<hr/>');
</script>
中国结
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>中国结</title>
<style>
*{
margin: 0;
padding: 0;
}
.wp{
/* border: 1px #f00 solid; */
text-align: center;
}
.wp1{
width: 30px;
height: 30px;
background: #f00;
margin: 5px;
display: inline-block;
}
</style>
</head>
<body>
</body>
</html>
<script>
// 中国结
for(let a=0;a<=12;a+=2){
document.write('<div class="wp">');
for(let b=0;b<=a;b++){
document.write('<div class="wp1"></div>');
}
document.write('</div>')
}
for(let a=11;a>=1;a-=2){
document.write('<div class="wp">');
for(let b=1;b<=a;b++){
document.write('<div class="wp1"></div>');
}
document.write('</div>')
}
</script>