JavaScript控制流、JavaScript DOM操作

JavaScript控制流 <script> let str = "icq123"; for (let index = 0; index < str.length; index++) { document.write(str[index]+"<br>"); } </script> let str = "icq123";
摘要由CSDN通过智能技术生成

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] + "&nbsp;&nbsp;&nbsp";
        }
        document.write(txt);
        let array=["name", 123, true, "123.com"];
        for (let index = 0; index < array.length; index++) {
            document.write(array[index]+"&nbsp;&nbsp;&nbsp;");
             }
        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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值