jquery事件绑定


window.onload 和 Jquery $(document).ready() 区别
* 1.用JavaScript的方式window.onload :必须等页面上所有的元素(css/js/图片)传递完毕之 后才去执行 绑定的函数中的代码 2.用JQuery的形式来实现,只要页面中的DOM树形成了,就开始执行(简单理解为传递完当前页面即可执行) * 有无简写形式 window.onload无简写形式 $(document).ready(function(){}) 有简写形式 $(function(){}); * 绑定的函数个数不一样 window.onload 只能绑定一个函数 $(function(){}); 可以绑定任意多个函数
window.onload 只能绑定一个函数,如下代码,只会执行two

<script type="text/javascript">
 function one(){
   alert("one");
 }
 function two(){
   alert("two");
 } 
 window.onload = one ;
 window.onload = two ;
</script>
$(function(){}); 可以绑定任意多个函数,如下代码都会执行

<script type="text/javascript">
  function one(){
   alert("one");
 }
 function two(){
   alert("two");
 } 
  $(document).ready(function(){
      one();
  })
  $(document).ready(function(){
      two();
  })
</script>

JQuery完善的事件处理机制,如果绑定失败,下边的代码也会执行,只是绑定的那段不会执行

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Panel</title>
<script src="../../jquery-1.4.2.js"></script>
<script type="text/javascript">
$(function(){
    $("#t").bind("click",function(){
        alert("zzzz");
    });
    alert("1111");
    alert("2222");
});
</script>
</head>
<body>
<pre id="tt">体会JQuery完善的事件处理机制</pre>
<pre>
    1.体会JQuery完善的事件处理机制
    2.为DM节点绑定事件
         $dm.bind("",function(){});
</pre>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4-2-1</title>
<link rel="stylesheet" type="text/css" href="../css/style.css" />
<script src="../../jquery-1.4.2.js"></script>
<script>
$(function(){
    /*
     * 获取id为panel下的class的值为head的h5元素,
     * 为这个元素用JQuery的原始方式绑定点击事件
     * 让当前元素的下一个兄弟节点显示
     */
    //$("#panel .head").bind("click",function(){
        //$(this).next().show();
    //});
    
    /*
      * 页面加载完毕之后为id为panel下的class值为head的h5元素用原始方式绑定一个click事件,
      * 获取当前节点的下一个兄弟元素$content
      * 当$coantent元素为的状态为可见状态是$dm.is(":visible")让节点$content节点隐藏.
      *    否则让节点$content节点xianshi.
      */
    /**
    $("#panel .head").bind("click",function(){
        var $con=$(this).next();
        if($con.is(":visible")){
            $con.hide(1000);
        }else{
            $con.show(1000);
        }
    });*/
    
    
    /*
      * 页面加载完毕之后,
      * 为id为panel下的class的值为head的h5元素用原始方式绑定mouseover事件,
      * 让当前节点的下一个兄弟节点显示
      * 
      * 为id为panel下的class的值为head的h5元素用原始方式绑定mouseout事件,
      * 让当前节点的下一个兄弟节点隐藏
      */
    
    /**
    $("#panel h5.head").bind("mouseover",function(){
        $("#panel h5.head").next().show();
    }).bind("mouseout",function(){
        $("#panel h5.head").next().hide();
    });*/
    
    /**
       * 等待页面加载完毕之后,
       * 为id为panel下的class值为head的h5元素用简写形式绑定一个mouseover事件,让当前节点的下一个兄弟节点显示
       * 为id为panel下的class值为head的h5元素用简写形式绑定一个mouseout事件,让当前节点的下一个兄弟节点隐藏
       */
//简写
$("#panel h5.head").mouseover(function(){ $("#panel h5.head").next().show(); }).mouseout(function(){ $("#panel h5.head").next().hide(); }); }); </script> </head> <body> <div id="panel"> <h5 class="head">什么是jQuery?</h5> <div class="content"> jQuery是继Prototype之后又一个优秀的JavaScript库,它是一个由 John Resig 创建于2006年1月的开源项目。jQuery凭借简洁的语法和跨平台的兼容性,极大地简化了JavaScript开发人员遍历HTML文档、操作DOM、处理事件、执行动画和开发Ajax。它独特而又优雅的代码风格改变了JavaScript程序员的设计思路和编写程序的方式。 </div> </div> <pre> 总结: 有哪些事件? 1.为节点绑定事件的方式 * $dm.bind("click",function(){ ... }); * $dm.click(function(){ ... }); $dm.click(function(){}); 2. $(this) 中this的含义,以及为什么需要将this用$()括起来 3. $dm.show(); $dm.hide(); 都属于节点的动画操作 , 都可以向其中传递参数,参数的形式为"3000","slow","normal","fast" </pre> </body> </html>
*{margin:0;padding:0;}    
body { font-size: 13px; line-height: 130%; padding: 60px }
#panel { width: 300px; border: 1px solid #0050D0 }
.head { padding: 5px; background: #96E555; cursor: pointer }
.content { padding: 10px; text-indent: 2em; border-top: 1px solid #0050D0;display:block;display:none; }

 发现我一朋友真是太有钱了,买件羽绒服就两万多,努力赚钱吧,以后买给自己家人

原文地址:https://www.cnblogs.com/MessiAndDream/p/6100216.html