实现图片点击切换、通过classList修改样式、操作表单元素属性、自定义属性

实现图片点击切换

html 复制代码
<!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>
<style>
    * {
        box-sizing: border-box;
    }

    .box {
        background-color: pink;
        width: 200px;
        height: 150px;
        border: 1px solid black;

    }
    #NameBox{
        font-size: 50px;
        text-align: center;
        line-height: 150px;
        color: azure;
    }
</style>

<body>
    <div class="box" id="NameBox"></div>
    <img src="../img/1.jpeg">


    <script>
        const box = document.querySelector('.box')
        const NameBox = document.getElementById('NameBox');
        box.style.width = '400px'

        function getRandom(N, M) {
            return Math.floor(Math.random() * (M - N + 1)) + N
        }

        const img = document.querySelector('img')//获取元素
        const random = getRandom(1, 6)
        img.src = `../img/${random}.jpeg`


        let num = 1;

        function showMessage() {
            num++;
            if (num % 5 === 0) {
                img.src = `../img/3.jpeg`
                NameBox.textContent = '图片3';
            } else if (num % 2 === 0) {
                img.src = `../img/2.jpeg`
                NameBox.textContent = '图片2';
            } else {
                img.src = `../img/1.jpeg`
                NameBox.textContent = '图片1';
            }
            console.log(num);
            // img.src = `../img/${num}.jpeg`

            

        }
        img.addEventListener('click', showMessage)//img
    </script>
</body>

</html>

通过classList修改样式

html 复制代码
<!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>
<style>
    .box{
        width: 200px;
        height: 200px;
        color: #333;
    }
    .active{
        color: red;
        background-color: pink;
    }
</style>
<body>
    <div class="box">文字</div>
    <script>
        //通过classList添加
        //获取元素
        const box = document.querySelector('.box')
        //修改样式 
        //追加类 add() 类名不加点 并且是字符串
        box.classList.add('active')
        //删除类 remove() 类名不加点 并且是字符串
        box.classList.remove('box')
        //切换类 toggle() 有就删掉,没有就加上
        box.classList.toggle('active')

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

操作表单元素属性




自定义属性


输出结果为


相关推荐
iReachers2 分钟前
HTML打包EXE配置管理教程:多项目打包设置一键保存、加载与切换
java·前端·javascript
武藤一雄5 分钟前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
霍理迪14 分钟前
Vue路由——route
前端
whuhewei17 分钟前
js事件循环
前端·javascript
TheRouter18 分钟前
构建一个支持多模型的 AI 聊天应用:React + TheRouter API 全栈教程
前端·人工智能·react.js
xiaofan110625 分钟前
Pretext:无 DOM 的多行文本测量与排版库
前端·javascript
yuki_uix29 分钟前
面试题里的 Custom Hook 思维:从三道题总结「异步状态管理」通用模式
前端·react.js·面试
cch891830 分钟前
Vue-Element-Admin快速上手指南
前端·javascript·vue.js
hzxpaipai33 分钟前
2026 杭州外贸网站制作公司哪家好?派迪科技确实有点技术
前端·科技·网络协议·网络安全
CHANG_THE_WORLD1 小时前
模拟解析:宽度数组 `[1,2,1]`,10个条目的 XRef 流
java·前端·算法