案例分析.

1.随机点名案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>
<body>
    <div>111</div>

    <script>

        let arr=['赵云','张飞','黄忠','关羽','马超','曹操']
        //1.获取元素
        const box=document.querySelector('div')
        //2.获取随机数   n-0   m---arr.length-1
        let random=Math.floor(Math.random()*arr.length)
        box.innerHTML=`${arr[random]}`
    </script>
</body>
</html>

2.修改元素属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 300px;
            height: 300px;
            background-color: aqua;
            font-size: 50px;
        }
    </style>

</head>
<body>
   <div class="box1">666666</div> 
   <div class="box2">666</div>

   <script>
    //1.获取元素
    const box1=document.querySelector('.box1')
    const box2=document.querySelector('.box2')
    //2.通过style修改样式
    box1.style.width='500px'
    box1.style.fontSize='16px'
    box1.style.backgroundColor='pink'
    //3.通过添加类名  className会将原来的类名删除掉不建议。
    //box2.className='box1'

    //classlist
    box2.classList.add('box1')  //追加
    box2.classList.remove('box1')//移除
    box2.classList.toggle('box1')//切换:有则删除,没有则添加

   </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值