jQuery事件
加载DOM
<script type="text/javascript"> function one() { alert("one"); } function two() { alert("two"); } /* jQery加载DOM就绪后就能注册事件,而JS加载DOM完成后才能注册事件 jQuery加载DOM就绪注册事件 */ //方式一 $(document).ready(function () { one(); }) $(document).ready(function () { two(); }) 方式二 $(function () { one(); }) $(function () { two(); }) //方式三 $().ready(function () { one(); }) $().ready(function () { two(); }) </script>
绑定事件
<script type="text/javascript"> //绑定单击事件 // $(function () { // $("#panel h5.head").bind("click", function () { //给<h5>绑定click事件 // //$(this).next("div.content").show(); //点击后显示文本内容 // if ($(this).next("div.content").is(":visible")) { //判断是否显示 // $(this).next("div.content").hide(); // } else { // $(this).next("div.content").show(); // } // }); // }); //绑定鼠标事件() // $(function () { // $("#panel h5.head").bind("mouseover", function () { //给<h5>绑定click事件 // $(this).next("div.content").show(); // }).bind("mouseout",function(){ // $(this).next("div.content").hide(); // }); // }); //简写上述事件 // $(function () { // $("#panel h5.head").mousemove(function () { // $(this).next("div.content").show(); // }).mouseout(function () { // $(this).next("div.content").hide(); // }); // }) //合成事件 //hover(function1(),function2()) //模拟光标悬停事件,移动到元素时触发第一个函数,移除元素时触发第二个函数 // $(function () { // $("#panel h5.head").hover(function () { // $(this).next("div.content").show(); // }, function () { // $(this).next("div.content").hide(); // }); // }); //toggle(fn1,fn2,fn3) //第一次单击触发第一个函数,第二次触发第二个,依次。。。 // $(function () { // $("#panel h5.head").toggle(function () { // $(this).next("div.content").show(); // }, function () { // $(this).next("div.content").hide(); // }); // }); //toggle() 点击切换元素的可见状态 $(function () { $("#panel h5.head").toggle(function () { $(this).addClass("highlight"); //添加class为"highlight"的样式 $(this).next("div.content").toggle(); }, function () { $(this).removeClass("highlight"); //移除class为"highlight"的样式 $(this).next("div.content").toggle(); }) }); //one()绑定事件,点击以后自动移除,只能点击一次 $(function () { $("#button").one("click", function () { //执行操作 }) }) //绑定多个事件 $(function () { $("div").bind("mouseover mouseout", function () { $(this).toggleClass("over"); //切换样式 }); }); </script>