javescript基础第一天

这篇博客介绍了如何使用JavaScript来操作DOM元素的样式,包括改变尺寸和背景颜色。通过监听按钮点击和鼠标悬停事件,实现了动态调整<div id='leftBox'>的宽高和背景色,提供了一个简单的交互式样式编辑界面。
摘要由CSDN通过智能技术生成

js第一天

js控制样式

基础
在这里插入图片描述

结构
<div id="wrap">
        <div id="leftBox"></div>
        <button id="rightBut">点击显示div样式</button>
        <div id="gray"></div>
        <div id="edit">
            <h2>大小</h2>
            <p>
                <a href="#" id="size1">300*300</a>
                <a href="#" id="size2">200*300</a>
                <a href="#" id="size3">300*200</a>
                <a href="#" id="size4">100*300</a>
            </p>
            <h2>颜色</h2>
            <p>
                <a href="#" id="color1">#ff0000</a>
                <a href="#" id="color2">#00ff00</a>
                <a href="#" id="color3">#0000ff</a>
                <a href="#" id="color4">#00ffff</a>
            </p>
            <button id="true">确定</button>
            <button id="false">取消</button>
        </div>
    </div>
样式
<style>
        *{padding: 0;margin: 0;text-decoration: none;}
        #wrap{
            position: relative;
            width: 500px;
            height: 300px;
            border: 1px solid gray;
            padding: 20px;
        }
        #leftBox{
            width: 300px;
            height: 100px;
            border: 1px solid gray;
        }
        #rightBut{
            position: absolute;
            top:20px;
            left:340px;
        }
        #gray{
            display: none;
            position: absolute;
            top:0;
            left: 0;
            width: 100%;
            height: 100%;
            background: black;
            opacity: 0.4;
        }
        #edit{
            display: none;
            position: absolute;
            top:20px;
            right:20px;
            width: 180px;
            height: 260px;
            padding: 10px;
            background: #fff;
        }
        #edit a{
            display: inline-block;
            color: #000;
            border: 1px solid gray;
            width: 80px;
            height: 25px;
            text-align: center;
            line-height: 25px;
            margin-bottom: 10px;
        }
    </style>

用js实现上面图片里面的样式

<script>
        var w = document.getElementById('wrap');
        var l = document.getElementById('leftBox');
        var r = document.getElementById('rightBut');
        var g = document.getElementById('gray');
        var e = document.getElementById('edit');
        var t = document.getElementById('true');
        var f = document.getElementById('false');
        var s1 = document.getElementById('size1');
        var s2 = document.getElementById('size2');
        var s3 = document.getElementById('size3');
        var s4 = document.getElementById('size4');
        var c1 = document.getElementById('color1');
        var c2 = document.getElementById('color2');
        var c3 = document.getElementById('color3');
        var c4 = document.getElementById('color4');

        r.onclick = function(){
            g.style.display = 'block';
            e.style.display = 'block';
        }
        t.onclick = function(){
            g.style.display = 'none';
            e.style.display = 'none';
        }
        s1.onmouseover = function(){
            l.style.width = '300px';
            l.style.height = '300px';
        }
        s1.onclick = function(){
            l.style.width = '300px';
            l.style.height = '300px';
        }
        s2.onmouseover = function(){
            l.style.width = '200px';
            l.style.height = '300px';
        }
        s2.onclick = function(){
            l.style.width = '200px';
            l.style.height = '300px';
        }
        s3.onmouseover = function(){
            l.style.width = '300px';
            l.style.height = '200px';
        }
        s3.onclick = function(){
            l.style.width = '300px';
            l.style.height = '200px';
        }
        s4.onmouseover = function(){
            l.style.width = '100px';
            l.style.height = '300px';
        }
        s4.onclick = function(){
            l.style.width = '100px';
            l.style.height = '300px';
        }
        c1.onmouseover = function(){
            l.style.background = "#ff0000";
        }
        c1.onclick = function(){
            l.style.background = "#ff0000";
        }
        c2.onmouseover = function(){
            l.style.background = "#00ff00";
        }
        c2.onclick = function(){
            l.style.background = "#00ff00";
        }
        c3.onmouseover = function(){
            l.style.background = "#0000ff";
        }
        c3.onclick = function(){
            l.style.background = "#0000ff";
        }
        c4.onmouseover = function(){
            l.style.background = "#00ffff";
        }
        c4.onclick = function(){
            l.style.background = "#00ffff";
        }
        f.onclick = function(){
            l.style = "";
            g.style.display = 'none';
            e.style.display = 'none';
        }


    </script>

先定义函数
var 名字 = document.getElementById(‘id名’);
声明事件,实现事件
f.onclick = function(){
l.style = “”;
g.style.display = ‘none’;
e.style.display = ‘none’;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值