JavaScript-17-事件

本文详细介绍了JavaScript中的事件触发机制,事件源以及各种事件类型,如window.onload、window.onscroll等。同时,提供了兼容不同浏览器的解决方案,并举例说明了onclick、onfocus、onchange等常见事件的使用。通过示例展示了如何检测用户名输入等交互功能。
摘要由CSDN通过智能技术生成

事件

  1. 事件触发机制

    1. 事件源:网页元素
    2. 事件
    3. 行为:函数
  2. 事件

    window事件

    1. window.onload 和 window.onunload 和 window.onbeforeunload

       window.onload=function(){
           console.log('onload....')
       };
       window.onbeforeunload=function(){
           console.log('before...unload...')
       };
       window.onunload=function () {
           console.log('unload......')
       };
      

      注意不能用alert(‘sss’);

    2. window.onscroll:页面滚动

       // 页面滚动控制导航
       <body>
           <div class="container">
               <header id="nav"></header>
               <div class="main"></div>
           </div>
       
       <script>
           var t_before=0;
           window.onscroll=function () {
               var t=document.documentElement.scrollTop || document.body.scrollTop;
               if (t>100 && t>t_before){
                   document.getElementById('nav').style.display='none';
               } else{
                   document.getElementById('nav').style.display='block';
               }
               t_before=t;
           }
       
       </script>
       </body>
      

      谷歌不识别document.documentElement.scrollTop,必须要加上document.body.scrollTop;如上

    3. window.onerror

    4. window.open(‘page.html’, ‘newwindow’, ‘height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no’) //该句写成一行代码

       window.open ("page.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") //写成一行
      
        参数解释:      
           window.open 弹出新窗口的命令; 
         'page.html' 弹出窗口的文件名; 
         'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替; 
         height=100 窗口高度; 
         width=400 窗口宽度; 
         top=0 窗口距离屏幕上方的象素值; 
         left=0 窗口距离屏幕左侧的象素值; 
         toolbar=no 是否显示工具栏,yes为显示; 
         menubar,scrollbars 表示菜单栏和滚动栏。 
         resizable=no 是否允许改变窗口大小,yes为允许; 
         location=no 是否显示地址栏,yes为允许; 
         status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许
      
    5. window.close() 关闭页面

    6. window.scrollTo()滚动到 window.scrollBy()滚动多少

    标签事件

    1. onclick

      几乎所有都可以onclick

    2. onfocus【onclick != onfocus】

      可以:文本框、按钮

      不可以:图片

      btn.focus()

    3. onblur:失去焦点

      检测用户名、密码…

      btn.blur()

    4. onchange:失去焦点+内容改变

      可以:input、select…

      不可以:按钮

      当文本框失去焦点后并且文本的内容发生变化时,触发该事件

       var text1=document.getElementById('txt01');
       text1.onchange=function () {
           alert(text1.value);
       }
      
    5. onmouseover

    6. onmouseout

       <div class="divcss" id="div01"></div>
       <div class="divcss" id="div02"></div>
      
       <script>
           var div01=document.getElementById('div01');
           var div02=document.getElementById('div02');
           div02.onmouseover=function () {
               div02.style.background='red';
       //            div01.style.background='yellow';
       //            document.body.style.background='grey'
               }
               div02.onmouseout=function () {
                   div02.style.background='blue';
       //            div01.style.background='black';
       //            document.body.style.background='white'
       
               }
       </script>
      
    7. onkeydown

    8. onkeyup

    9. onkeypress = onkeydown + onkeyup

        text1.onkeyup=function (event) {
          
      // key表示按键的内容(区分大小写)
            alert(event.key);
      //keycode表示按钮的ascii码
           alert(event.keyCode);
      // 回车的asci是13
           if(event.keyCode==13){
                  text1.blur();
            }
      }
      

    补充

    对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:

    window.innerHeight - 浏览器窗口的可见高度
    window.innerWidth - 浏览器窗口的可见宽度

    对于 Internet Explorer 8、7、6、5:
    document.documentElement.clientHeight
    document.documentElement.clientWidth
    或者
    document.body.clientHeight
    document.body.clientWidth

    兼容所有浏览器:

     var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    
     window.onscroll = function(){
     var a = document.documentElement.scrollTop || document.body.scrollTop;//滚动条y轴上的距离
     var b = document.documentElement.clientHeight || document.body.clientHeight;//可视区域的高度
     var c = document.documentElement.scrollHeight || document.body.scrollHeight;//可视化的高度与溢出的距离(总高度)
    
     console.log('a=',a);
     console.log('b=',b);
     console.log('c=',c);
     }
    

小实例

  1. 测试用户名输入框

     <body>
     <input type="text" id="user_id" required><br/>
     <input type="text" id="user_psw"><br/>
     <span id="info"></span>
     
     <script>
         var user_id=document.getElementById('user_id');
         var user_psw=document.getElementById('user_psw');
         var info=document.getElementById('info');
     
         user_id.focus();
         user_id.onchange=function(){
             if (this.value.length<6){
                 info.innerText='用户名长度不够';
             }else{
                 info.innerText='';
             }
         };
         user_id.onkeypress=function(ev){
             if (ev.key.toLowerCase()=='enter'){
                 user_psw.focus(); /* 下个获取焦点,this 自动失去 */
             }
         };
     </script>
    

2…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值