JQuery 事件相关内容练习1

 1.$(document).ready()和window.onload方法的比较

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>$()和window.onload区别</title>
<style type="text/css">
 
</style>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
/*
 window.οnlοad=function(){
     alert($("img").width());
 }
 */
 /*
 $(function(){
    alert($("img").width()); 
 });
 */
 /*
 $(window).load(function(){
     alert($("img").width());
 });
 */
 /*
 window.οnlοad=function(){
    alert("hello1");
 }
  window.οnlοad=function(){
    alert("hello2");//只能绑定一个函数的使用,最终输出hello2
 }
 */
 $(function(){
    alert("hello1"); //能绑定多个函数,执行多次
 });
  $(function(){
    alert("hello2"); 
 });
</script>
</head>
<body>
  <img src="jia.jpg" width="100px" height="100px"/>
</body>
</html>

截图:


2.事件的绑定

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>绑定事件</title>
<style type="text/css">
 table{
     border:0px;
     border-collapse:collapse; 
 }
 td{
     font:Arial, Helvetica, sans-serif;
      padding:2px; 
      width:100px;
 }
 th{
     font:Arial, Helvetica, sans-serif;
     text-align:left;
     padding:4px;
     border-bottom:1px solid #0FF;
 }
 .even{
     background:#069;
 }
 .odd{
     background:#906;
 }
 .selected{
     background:#FF3;
     color:#FFF;
 }
 .over{
     background:#0F0;
 }
</style>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
 $(function(){
     $("tbody>tr:odd").addClass("odd");
     $("tbody>tr:even").addClass("even");
     //bind可以绑定多个事件,mouseover和click事件
     //第一种方法
     /*
     $("tbody>tr").bind("click",function(){
        var hasSelected=$(this).hasClass("selected");
        if(hasSelected){
           $(this).removeClass("selected").find(":checkbox").attr("checked",false);    //点击事件时添加的属性和选择器
        }
        else{
             $(this).addClass("selected").find(":checkbox").attr("checked",true);    
        }
     }).bind("mouseover",function(){
         $(this).addClass("over").siblings().removeClass("over");//背景色改成绿颜色
     });
     */
     //第二种方法
     $("tbody>tr").click("click",function(){
        var hasSelected=$(this).hasClass("selected");
        if(hasSelected){
           $(this).removeClass("selected").find(":checkbox").attr("checked",false);    //点击事件时添加的属性和选择器
        }
        else{
             $(this).addClass("selected").find(":checkbox").attr("checked",true);    
        }
     }).mouseover(function(){
         $(this).addClass("over").siblings().removeClass("over");//背景色改成绿颜色
     });
     $("tbody>tr:has(':checked')").addClass("selected");
 });
</script>
</head>
<body>
 <table>
   <thead>
     <tr><th></th><th>姓名</th><th>性别</th><th>暂住地</th></tr>
   </thead>
   
   <tbody>
    <tr><td><input type="checkbox" name="choice" value=""/></td>
         <td>张三</td><td>男</td><td>浙江宁波</td></tr>
    <tr><td><input type="checkbox" name="choice" value=""/></td>
         <td>李四</td><td>女</td><td>浙江杭州</td></tr>
    <tr><td><input type="checkbox" name="choice" value="" checked='checked'/></td>
         <td>王五</td><td>男</td><td>湖南长沙</td></tr>
    <tr><td><input type="checkbox" name="choice" value=""/></td>
         <td>赵六</td><td>男</td><td>浙江温州</td></tr>
    <tr><td><input type="checkbox" name="choice" value=""/></td>
         <td>Rain</td><td>男</td><td>浙江杭州</td></tr>
    <tr><td><input type="checkbox" name="choice" value=""/></td>
         <td>MAXMAN</td><td>女</td><td>浙江宁波</td></tr>
   </tbody>
 </table>
</body>
</html>

截图:


3.hover事件的点击

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>hover事件</title>
<style type="text/css">
 *{
     margin:0px;
     padding:0px; 
 }
 body{
     font-size:14px;
     line-height:130%;
     padding:60px; 
 }
 #pane1{
     width:300px;
     border:1px solid #0FF;
 }
 .head{
     height:24px;
     line-height:24px;
     text-indent:10px;
     background:#00F;
     cursor:pointer;
     width:100%;
 }
 .content{
     padding:10px;
     text-align:24px;
     border-top:1px solid #903;
     display:none;
     border:1px solid #99C;
 }
</style>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
 $(function(){
    $(".head").hover(
    function(){
        $(this).next().show();
        },
        function(){
            $(this).next().hide();
            })
 });
</script>
</head>
<body>
  <div id="pand1">
    <h5 class="head">什么是jQuery?</h5>
    <div class="content">
     jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架)。jQuery设计的宗旨是"write Less,DoMore",即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。
    </div>
  </div>
</body>
</html>

截图:


4.toggle事件的点击

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>toggle事件</title>
<style type="text/css">
 *{
     margin:0px;
     padding:0px; 
 }
 body{
     font-size:14px;
     line-height:130%;
     padding:60px; 
 }
 #pane1{
     width:300px;
     border:1px solid #0FF;
 }
 .head{
     height:24px;
     line-height:24px;
     text-indent:10px;
     background:#00F;
     cursor:pointer;
     width:100%;
 }
 .content{
     padding:10px;
     text-align:24px;
     border-top:1px solid #903;
     display:none;
     border:1px solid #99C;
 }
</style>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
 $(function(){
    $(".head").click(function(){
        $(this).next().toggle();//点击出现,再点击消失
    });
 });
</script>
</head>
<body>
  <div id="pand1">
    <h5 class="head">什么是jQuery?</h5>
    <div class="content">
     jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架)。jQuery设计的宗旨是"write Less,DoMore",即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。
    </div>
  </div>
</body>
</html>

截图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

潇潇雨歇_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值