事件

事件

达到了某个预先设定的条件 自动触发的动作

绑定事件的两种方法
<button onclick="func1()">点我</button>
<button id="d1">点我</button>

<script>
    // 第一种绑定事件的方式
   function func1() {
        alert(111)
   }
    // 第二种
   let btnEle = document.getElementById('d1');
    btnEle.onclick = function () {
        alert(222)
   }
</script>

'''
ps:script通常丢在body内的最底部 放在head内也可以

# 等待浏览器窗口加载完毕之后再执行代码
window.onload = function () {
           // 第一种绑定事件的方式
          function func1() {
               alert(111)
           }
           // 第二种
          let btnEle = document.getElementById('d1');
           btnEle.onclick = function () {
               alert(222)
           }
       }
"""
'''

原生的JS事件绑定

看懂即可

  • 开关灯案例

<div id="d1" class="c1 bg_red bg_green"></div>
    <button id="d2">变色</button>

    <script>
        let btnEle = document.getElementById('d2')
        let divEle = document.getElementById('d1')
        btnEle.onclick = function () {  // 绑定点击事件
           // 动态的修改div标签的类属性
           divEle.classList.toggle('bg_red')
       }
    </script>
  • input框获取焦点失去焦点案例

<input type="text" value="老板 去吗?" id="d1">

<script>
    let iEle = document.getElementById('d1')
    // 获取焦点事件
   iEle.onfocus = function () {
        // 将input框内部值去除
       iEle.value = ''
        // 点value就是获取   等号赋值就是设置
  }
    // 失去焦点事件
   iEle.onblur = function () {
        // 给input标签重写赋值
       iEle.value = '没钱 不去!'
   }
</script>
  • 实时展示当前时间

<input type="text" id="d1" style="display: block;height: 50px;width: 200px">
<button id="d2">开始</button>
<button id="d3">结束</button>

<script>
    // 先定义一个全局存储定时器的变量
   let t = null
    let inputEle = document.getElementById('d1')
    let startBtnEle = document.getElementById('d2')
    let endBtnEle = document.getElementById('d3')
    // 1 访问页面之后 将访问的时间展示到input框中
   // 2 动态展示当前时间
   // 3 页面上加两个按钮 一个开始 一个结束
   function showTime() {
        let currentTime = new Date();
        inputEle.value = currentTime.toLocaleString()
   }

    startBtnEle.onclick = function () {
        // 限制定时器只能开一个
       if(!t){
            t = setInterval(showTime,1000// 每点击一次就会开设一个定时器 而t只指代最后一个
      }
   }
    endBtnEle.onclick = function () {
        clearInterval(t)
        // 还应该将t重置为空
       t = null
   }
</script>
  • 省市联动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<select name="" id="d1">
    <option value="" selected disabled>--请选择--</option>
</select>
<select name="" id="d2"></select>

<script>
    let proEle = document.getElementById('d1')
    let cityEle = document.getElementById('d2')
    // 先模拟省市数据
   data = {
        "河北": ["廊坊", "邯郸",'唐山'],
        "北京": ["朝阳区", "海淀区",'昌平区'],
        "山东": ["威海市", "烟台市",'临沂市'],
        '上海':['浦东新区','静安区','黄浦区'],
        '深圳':['南山区','宝安区','福田区']
   };
    // 选for循环获取省
   for(let key in data){
        // 将省信息做成一个个option标签 添加到第一个select框中
       // 1 创建option标签
       let opEle = document.createElement('option')
        // 2 设置文本
       opEle.innerText = key
        // 3 设置value
        opEle.value = key  // <option value="省">省</option>
        // 4 将创建好的option标签添加到第一个select中
       proEle.appendChild(opEle)
   }
    // 文本域变化事件 change事件
   proEle.onchange = function () {
        // 先获取到用户选择的省
       let currentPro = proEle.value
        // 获取对应的市信息
       let currentCityList = data[currentPro]
        // 清空市select中所有的option
        cityEle.innerHTML = ''
        // 自己加一个请选择
       let ss = "<option disabled selected>请选择</option>"
        // let oppEle = document.createElement('option')
        // oppEle.innerText = '请选择'
        // oppEle.setAttribute('selected','selected')
        cityEle.innerHTML = ss

        // for循环所有的市 渲染到第二个select中
       for (let i=0;i<currentCityList.length;i++){
            let currentCity = currentCityList[i]
            // 1 创建option标签
           let opEle = document.createElement('option')
            // 2 设置文本
           opEle.innerText = currentCity
            // 3 设置value
            opEle.value = currentCity  // <option value="省">省</option>
            // 4 将创建好的option标签添加到第一个select中
           cityEle.appendChild(opEle)
       }
   }
</script>
</body>
</html>

posted @ 2020-05-19 19:54  江湖有梦  阅读(128)  评论(0编辑  收藏  举报