JavaScript基础知识---事件

事件

事件中有:事件源  事件名   事件注册   事件处理

常用的事件

鼠标常用事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>鼠标事件</h2>
<button onclick="fun()">请点击 鼠标单击</button>
<br>
<button ondblclick="fun()">请点击 鼠标双击</button>
<br>
<button onmousedown="fun()">请点击 鼠标按下</button>
<br>
<button onmouseup="fun()">请点击 鼠标弹起</button>
<br>
<button onmouseenter="fun()">请点击 鼠标进入</button>
<br>
<button onmouseleave="fun()">请点击 鼠标离开</button>

</body>
<script>
    function fun() {
        alert("surprise moti!");
    }
  

</script>
</html>

 键盘事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>键盘事件</h2>
键盘按下,获取键码:<input type="text" onkeydown="fun1(this)">
<br>
键盘松开,获取键码:<input type="text" onkeyup="fun1(this)">
<br>

</body>
<script>
   
    function fun1(e) {
        alert("surprise moti!"+e.type);
    }

    

</script>
</html>

事件注册

方法一:写在标签中,如上面的案例

方法二:通过JS去设置OnClick属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>



<h2>事件注册</h2>
事件注册1:<input type="text" id="input1"><br>
</body>
<script>
    function fun() {
        alert("surprise moti!");
    }
 
    //事件注册
    var input = document.getElementById("input1");
    input.onfocus = fun();

</script>
</html>

方法三:通过addEventListener注册

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<h2>事件注册</h2>
事件注册1:<input type="text" id="input1"><br>
事件注册2:<input type="text" id="input2"><br>
</body>
<script>
    function fun() {
        alert("surprise moti!");
    }
    function fun1() {
        alert("修改!");
    }

    //事件注册
    var input = document.getElementById("input1");
    input.onfocus = fun();

    var input2 = document.getElementById("input2");
    input2.addEventListener("change",function (ev) {
        alert("值修改了");
    })
    input2.addEventListener("change",fun1);
    //移除事件
    input2.removeEventListener("change",fun1);

</script>
</html>

推荐使用后两种方法,将页面内容和行为分开了

事件捕获与冒泡

如果2个div是父子关系,点击里头那个div,也会触发父亲那个div的点击事件,,

2个div点击事件都会触发,我们怎么去规定它的处理的顺序

冒泡:按照从内往外的方式依次处理,默认方式

捕获:反之

阻止冒泡:ev.stopPropagation();

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件捕获与冒泡</title>
    <style>
        .div1{
            height: 200px;
            width: 200px;
            background-color: yellow;
        }
        .div2{
            height: 100px;
            width: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
<div id = "div1" class="div1">
    <div id = "div2" class="div2">   </div>
</div>
</body>
<script>
    //true 与false决定是从里向外执行还是从外向里执行

    var div1 = document.getElementById("div1");
    var div2 = document.getElementById("div2");
    div1.addEventListener("click",function (ev) {
        console.log("我是div1");
    },false);
    div2.addEventListener("click",function (ev) {
        console.log("我是div2");
        ev.stopPropagation();//阻止冒泡
    },false);
</script>
</html>

DOM

Document Object Mode:文档对象类型

整个文档是由一序列节点对象组成的一棵树

获取元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dom获取元素</title>
    <style>
        .div1{
            height: 200px;
            width: 200px;
            background-color: yellow;
        }
        .div2{
            height: 100px;
            width: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
<div id = "div1" class="div1" name="divs">
    <div id = "div2" class="div2" name="divs">   </div>
</div>
</body>
<script>
    //获取指定元素
    var div1 = document.getElementById("div1");

    var div2 = document.getElementById("div2");
    //修改容器值
    div2.innerHTML="你是2号";

    //获取元素通过标签名 返回元素集合
    var div = document.getElementsByTagName("div");
    console.log(div.length);
    //获取元素通过name 返回元素集合
    var div2 = document.getElementsByName("divs");
    console.log(div2.length);

    //获取元素通过class 返回元素集合
    var div3 = document.getElementsByClassName("div1");
    console.log(div3.length);

</script>
</html>

修改元素,获取标签内容,修改元素样式,添加(删除)元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dom修改元素</title>
    <style>
        .div1{
            height: 200px;
            width: 200px;
            background-color: yellow;
        }
        .div2{
            height: 100px;
            width: 100px;
            background-color: red;

        }
        .div2-hiddle{
            display: none;
        }
    </style>
    <script src="../js/hello.js"></script>
</head>
<body>
<div id = "div1" class="div1" name="divs">
    <div id = "div2" class="div2" name="divs">我是第二号</div>
</div>
</body>
<script>
    //获取指定元素
    var div1 = document.getElementById("div1");
    var div2 = document.getElementById("div2");
    //修改值 ,如果有嵌套标题,父类修改内容之后,原有子标签会消失
    // div1.innerHTML="<img src='../img/图片3.png' />"
    // div2.innerHTML="<img src='../img/图片3.png' />";

    //获取文本
    console.log(div2.innerText);
    console.log(div2.innerHTML);

    // 修改元素样式
    div1.addEventListener("click",function (ev) {
        var div2 = document.getElementById("div2");
        if(div2.style.display =="none"){
            div2.style.display ="block";
        }
        else {
            div2.style.display ="none"
        }
    });

    div2.addEventListener("click",function (ev) {
        var div2 = document.getElementById("div2");
        div2.className = div2.className +" div2-hiddle";
        var p = document.createElement("p");
        p.innerText = "明天放假!";
        div1.append(p);
    });


</script>
</html>

BOM

Browser object Model

常用对象

Window:

窗口对象,浏览器窗口

我们定义的全局变量和全局函数都是Window对象的属性和方法

       Window.xx = “aa”

打开一个新的窗口

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Bom</title>
</head>
<body>

</body>
<script>
    var myWindow = window.open("https://www.baidu.com",
                                "_black","width=200,height=200");
</script>
</html>

lcation

window.location和直接写location

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Bom</title>
</head>
<body>

</body>
<script>
    console.log(window.location.href);//获取当前url
    console.log(window.location.port);//获取端口号
    console.log(window.location.host);//获取主机名
    console.log(window.location.protocol);//获取传输协议
    window.location.href="https://www.baidu.com";//修改href页面跳转
    location.reload(true);//重新加载页面
</script>
</html>

History

历史记录对象,它是一个集合数组,无法读取到里面的值。

Back();

Forward();

History.go(-2)

cookie

作用:在本地浏览器存储数据,常用于记住账号

cookie组成:

         键值对形式存储

         存储数据:“userId=177171;password=123456”

         有效期:“expires=今天以后的时间”

         Cookie可以保持多个键值对

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Cookie</title>
</head>
<body>

</body>
<script>
    var date = new Date();
    date.setDate(date.getDate()+7);
    //建立模拟数据
    var userId = "1234611";
    var password = "123456";
    //存到cookie中
    var context1 = "userId="+userId+";expires="+date;
    var context2 = "password="+password+";expires="+date;
    document.cookie = context1;
    document.cookie = context2;
</script>
</html>

效果如下:

所有的网络请求都会将cookie数据带上,发送给后台。

Cookie存储数据是有限的。

SessionStorage和localStorage

localStorage:长期有效,除非移除

sessionStorage:本次会话有效,浏览器关闭就没了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Cookie</title>
</head>
<body>

</body>
<script>
   

    //存储数据h5的新特性
    localStorage.setItem("name","zx");
    console.log(localStorage.getItem("name"));

    sessionStorage.setItem("name","ls");
    console.log(sessionStorage.getItem("name"));

</script>
</html>

计时器和延时器

计时器:每隔多少时间,重复干什么事情

延时器:隔多次时间在调用什么函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计时器演延时器</title>
</head>
<body>
<div id = "count">
    0
</div>
</body>
<script>
    var count = document.getElementById("count");
    var countTime = setInterval(function () {
        count.innerText = parseInt(count.innerText )+ 1;
        //移除计时器
        if(parseInt(count.innerText )> 100){
            clearInterval(countTime);
        }
    },100);
    //延时器 隔多久后执行什么操作
    setTimeout(function () {
        //移除计时器
        clearInterval(countTime);
    },1000);


</script>
</html>

数据交换格式

前端和后端,他们自己有数据交互,

前端的数据需要提交给后端

 后端的数据也需要返回给前端  他们数据交换的格式称为数据交换格式

注意:前后端数据交换方式是HTTP/HTTPS,数据交换格式是JSON/XML

JSON

就是键值对方式;

JSON对象{ }

如果里面的值是数组:[]

定义方式以及常用方法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>json</title>
</head>
<body>

</body>
<script>
    //定义个json对象
    var json = {
        name:"zs",
        list:[
            {pId:1,pName:"car"},
            {pId:2,pName:"bike"},
            {pId:3,pName:"football"}
        ]
    };

    //json对象转换为json字符串
    var jstr = JSON.stringify(json);
    console.log(jstr);
    //json字符串转换为JSON对象
    var ojson = JSON.parse(jstr);
    console.log(ojson);
</script>
</html>

XML

总结

    可以看到,JSON 简单的语法格式和清晰的层次结构明显要比 XML 容易阅读,并且在数据交换方面,由于 JSON 所使用的字符要比 XML 少得多,可以大大得节约传输数据所占用的带宽。

Ajax

异步请求方式,在一些网站可以实现异步刷新,在后台偷偷的跑,

比如:加载一个页面,

          静态的资源(html,css,js)

          页面上要显示的内容通过ajax去访问,得到数据之后,然后将数据展示在页面上。

此处只简单的做一个介绍,具体的使用要结合案例才能更好的理解。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名一小卒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值