JS基础入门篇(二十)—事件对象以及案例(二)

案例一.点击按钮,选中input中的全部内容

select()方法:选中全部。

点击按钮选中输入框中的内容!!!!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        input,
        button{
            margin: 0;
            padding: 0;
            outline: none;
        }
        input{
            width:200px;
            height:40px;
            border:1px solid black;
            font-size: 20px;
        }
        button{
            width: 300px;
            line-height: 40px;
            vertical-align: top;
        }
    </style>
</head>
<body>
    <input type="text">
    <button>点击此按钮选中input的全部内容</button>
    <script>
        var input=document.getElementsByTagName("input")[0];
        var button=document.getElementsByTagName("button")[0];
        button.onclick=function () {
            input.select();
        }
    </script>
</body>
</html>

案例二.点击按钮,选中input中的全部内容,并复制到粘贴板上。

document.execCommand("copy");//复制到粘贴板上。

点击按钮,选中并复制到粘贴板上,点击查看效果以及代码

<body>
<input type="text">
<button>点击此按钮选中input的全部内容</button>
<script>
    var input=document.getElementsByTagName("input")[0];
    var button=document.getElementsByTagName("button")[0];
    button.onclick=function () {
        input.select();//选中input的所有内容
        document.execCommand("copy");//复制到粘贴板上
    }
</script>
</body>

事件对象

事件:当用户对页面进行操作的交互时,会触发对应元素的事件。
事件对象:

        event
            当发生事件,执行事件处理函数的时候,该时刻的详细信息。
        注意:如果函数是直接调用的,则没有事件对象
        注意:不同事件中的event对象可能有不同

举例说明:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
    #box{
        width:100px;
        height:100px;
        background-color:red;
    }
</style>
</head>
<body>
<div id="box"></div>
<script>
    var box = document.getElementById("box");
//    box.onmouseover = fn;//打印undefined
//    box.onmousedown = fn;//打印undefined
    document.onkeydown = fn;//打印按下的键值
//    fn();//报错
    function fn(){
//        console.log( typeof event );
        console.log( event.keyCode );
    }
</script>

</body>
</html>

案例三:模拟苹果电脑菜单

点击查看效果以及代码!!!!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
            padding: 0;
        }
        .wrap{
            border:1px solid black;
            position: absolute;
            bottom:100px;
            text-align: center;
            width: 100%;
        }
        .wrap  div{
            width: 100px;
            height: 100px;
            background: cornflowerblue;
            display: inline-block;
            vertical-align: bottom;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
    <script>
        var wrap=document.getElementsByClassName("wrap")[0];
        var divs=wrap.getElementsByTagName("div");
        document.onmousemove=function () {
            for(var i=0;i<divs.length;i++){
                calc( divs[i],event);
            }
        }
        function calc( obj,mouseEvent ){
            var objPos = {
                //offsetLeft:相对于最近定位父级定位元素
                x: obj.offsetLeft + 50,
                //obj.getBoundingClientRect().top:距离文档顶部位置
                y: obj.getBoundingClientRect().top + 50
            }//元素中心点坐标
            var mosPos = {
                x: mouseEvent.clientX,
                y: mouseEvent.clientY
            }//鼠标所在位置
            var dis = Math.sqrt( Math.pow( objPos.x-mosPos.x,2 ) + Math.pow( objPos.y-mosPos.y,2 ) );
            var scale = 1;
            //当鼠标离原点中心距离小于200时,则放大
            if( dis < 200 ){
                scale = (200 - dis) / 200 + 1;
            }
            obj.style.width = 100 * scale + "px";
            obj.style.height = 100 * scale + "px";
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值