js案例-属于你的大型选秀现场

一想到古代的皇帝,后宫佳丽三千人,那个羡慕嫉妒啊!今天,你也可以拥有你的后宫佳丽,冲鸭!!!
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>时间</h3>
<input type="text" value="" id="time">
<button onclick="startTime()">开始</button>
<button onclick="stopTime()">停止</button>
<h3>大型选秀抽奖现场</h3>
<button onclick="start1()">开始</button>
<button onclick="stop1()">停止</button>选中的人有:<input type="text"  id="inp1" size="50"><button onclick="quchong()">去重</button>
<div id="div1" style="border: 1px solid red;width: 500px;height: 420px;background: url(../img/妲己.jpg)  no-repeat;
background-size: 500px 420px">
</div>
</body>
<script>
    let time = document.getElementById("time");

    function startTime(){
        s=setInterval(showTime,1000)
    }
    function showTime(){
        let date = new Date();
        date1=date.toLocaleString()
        time.value=date1
        // console.log(typeof(date))
        // console.log(date.toLocaleString())

        if(date.getSeconds()==0){
            start1()
        }
        if(date.getSeconds()==5){
            stop1()
        }

    }

    function stopTime(){
        clearInterval(s)
    }




    let img=['貂蝉','金莲','妲己','西施']
    function start1() {
        s=setInterval(showBeauty,100)
    }
    function showBeauty() {
        ran=Math.floor(Math.random()*4)
        div1.style.backgroundImage="url(../img/"+img[ran]+".jpg)"

    }
    function stop1() {

        clearInterval(s)
        inp1.value+=img[ran]+','
    }




    function quchong() {
        arr1 = inp1.value.split(',');
        // console.log(arr1);
        let arr2=[]
        for(let i=0;i<arr1.length-1;i++){
            let flag=false //这里定义false为不存在
            for(let name of arr2 ){
                if(name==arr1[i]){
                    //如果有重复的,则定义为true,不加入新数组中
                    flag=true
                    break
                }
            }
            if(!flag){  //如果不存在,才放入
                arr2.push(arr1[i])
            }
        }
        //将新数组加入到value中
        console.log(arr2);
        inp1.value=arr2+','
    }
</script>

</html>

效果图

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值