欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JQuery中的事件及動畫用法實例

 更新時間:2015年01月26日 11:25:00   投稿:shichen2014  
這篇文章主要介紹了JQuery中的事件及動畫用法,實例分析了事件的綁定、移除、添加及動畫方法操作表單的使用技巧,需要的朋友可以參考下

本文實例講述了JQuery中的事件及動畫用法。分享給大家供大家參考。具體分析如下:

1.bind事件

復(fù)制代碼 代碼如下:
<script src="script/jquery-1.7.1.min.js"></script>
<script>
$(function () {
$("#divid h5.head").bind("click", function () {  //bind事件,其中包含三個參數(shù),第一個為事件,第二個為事件
alert($(this).text());
});
$("#divid h5.content").css("display", "none");    //css方法就是可以動態(tài)設(shè)置標(biāo)簽樣式
});
$(function () {
$("#btnid").bind("click", function () {
if (bool == true) {
$("#btnid .content").css("display", "none");
bool = false;
$(this).val("顯示");
}
else {
$("#btnid .content").css("display", "");
bool = true;
$(this).val("隱藏");
}
});
});
$(function () {
$("input[type=button]").bind("click", function () {  //內(nèi)容的顯示與隱藏
var content = $("#divid .content");
if (content.is(":visible")) {
content.hide();
$(this).val("顯示");
}
else {
content.show();
$(this).val("隱藏");
}
});
});
</script>
<body>
<div id="divid">
<h5 class="head">Rocky?</h5>
<div class="content">就讓雨下下來 不用帶傘 讓一切完蛋 看被淋濕的心 多久才會曬干</div>
</div>
<input type="button" name="name" value="顯示 " id="btnid" />
</body>

在上面的操作中我們新學(xué)習(xí)了bind事件,而bind事件是三個參數(shù),第一個參數(shù)是事件的名字,例如:click,dbclick,mouseover等,第二個參數(shù)是data,即傳遞過來的事件對象,第三個參數(shù)是一個方法,即用來處理處 理綁定的事件函數(shù)這就是我們的一個特殊的事件;另外在這里還舉例寫了一個動畫中的例子,即文本信息的顯示或者隱藏,在還沒有學(xué)習(xí)show()和 hide()之前我們一般是按照上面第一種方式來寫的,定義一個bool類型的變量即可,這樣寫起來還是很簡單的,但是在寫顯示隱藏時間處理按鈕上面還是 蠻蠻煩的,所以在學(xué)習(xí)了show()和hide()后就簡單許多了,就是直接可以隱藏和顯示??梢詫Ρ纫幌拢@然在代碼的處理上簡單啦。

2.toggle事件和事件冒泡

復(fù)制代碼 代碼如下:
<script>
$(function () {
$("input[type=button]").toggle(function () {    //toggle兩個參數(shù)都為事件,輪番調(diào)用
$(this).css("backgroundColor","red");
}, function () {
$(this).css("backgroundColor", "yellow");
});
});
$(function () {
$("div").each(function () {
$(this).bind("mouseup", function (e) {
alert(e.pageX);   //輸出鼠標(biāo)的x方向的位置
alert(e.pageY);   //輸出鼠標(biāo)的y方向的位置
alert(e.which);   //輸出鼠標(biāo)的按鍵的選擇,1為鼠標(biāo)左鍵,2為滾軸按鍵,3為鼠標(biāo)右鍵
});
});
});
$(function () {
$("#txt").keydown(function () {
e.preventDefault();      //阻止a標(biāo)簽鏈接
alert(e.keyCode);           //鍵盤獲取其ask碼
});
});
$(function () {
$("#ouuerdiv").click(function () {
alert($(this).text());
});
$("#div").click(function () {
alert($(this).text());
});
$("#innerdiv").click(function () {          //在這里是寫了一個事件的冒泡現(xiàn)象,組織冒泡可以使用preventDefault或者precentDefault
alert($(this).text());
});
})
</script>
<body>
<input type="button" name="btnname" value="按鈕" id="btn"/>
<div id="ouuerdiv"> 外部div<div id="div">中部div<div id="innerdiv">內(nèi)部div</div></div></div>
<a id="a">百度</a>
<textarea id="txt" rows="5" cols="5">
</textarea>
</body>

Toggle事件:模擬鼠標(biāo)點擊事件,當(dāng)鼠標(biāo)移動到元素上時觸發(fā)第一個事件,當(dāng)鼠標(biāo)離開元素時觸發(fā)第二個事件。兩個事件之間相互切換觸發(fā);另外還要說下事 件冒泡,事件冒泡其實簡單的理解為:在一個頁面上可以有多個事件,也可以多個元素相應(yīng)一個事件。像上面一樣假設(shè)頁面中存在兩個元素,其中一個div元素嵌 套在另一個div元素中并且都綁定了一個click事件,那么當(dāng)你點擊內(nèi)部中div元素時間,外部的div也會顯示,這就是事件冒泡。在這里需要注意的是都綁定了一個事件,容易想當(dāng)然的認(rèn)為僅僅的內(nèi)部發(fā)生click事件。

3.移除事件和連續(xù)添加多個事件

復(fù)制代碼 代碼如下:
<script>
$(function () {
$("removeall").click(function () {     
$("#btn").unbind();                //實現(xiàn)移除事件
});
$("#btn").bind("click", function () {          //可以連續(xù)添加多個事件
$("#text").append("<p>我是第一個添加的事件</p>")
})
.bind("click", function () {
$("#text").append("<p>我是第二個添加的事件</p>")
})
.bind("click", function () {
$("#text").append("<p>我是第三個添加的事件</p>")
})
});
</script>
<body>
<button id="btn">單擊我吧</button><button id="removeall">刪除所有的事件</button>
<div id="text">div文本信息</div>
</body>

上面我們學(xué)習(xí)了bind事件,就是添加一個事件,而unbind就是移除事件,我們可以對比一下,嘿嘿,而針對連續(xù)添加多個事件其實就是當(dāng)你添加玩一個事件后繼續(xù).bind添加事件即可。

4.模擬事件

我們學(xué)習(xí)的上面的bind事件、click事件等一般都是通過單擊按鈕才能觸發(fā)的事件,但是有時間,需要通過模擬用戶操作,來達(dá)到單擊的效果,例如:在用戶進(jìn)入也買年后就觸發(fā)click事件,而不需要用戶去單擊,那么我們就使用trigger()方法來完成模擬操作。

5.一些其他的事件

復(fù)制代碼 代碼如下:
<script>
$(function () {
$("#btn").click(function () {
//$("#div").hide(2000);        //在2秒內(nèi)隱藏
//$("#div").show(2000);        //在2秒內(nèi)顯示
//$("#div").fadeIn(2000);      //增強(qiáng)元素的不透明度,直至元素完全顯示
//$("#div").fadeOut(2000);     //降低元素的不透明度,直至元素完全消失
$("#btn").toggle(function () {
$("div").slideDown(2000);     //改變元素的高度,由上至下顯示
$(this).val("顯示")        
}, function () {
$("div").slideUp(2000);       //改變元素的高度,由下至上縮短隱藏
$(this).val("隱藏")
});
});
//$("#btn").click(function () {
//    $("div").fadeTo(600,0.2);       //fadeTo方法適用于在0.6s內(nèi)透明度是0.2
//});
});
</script>
<body>
<div id="div" style="width:300px; height:300px;" >1234</div>
<input type="button" name="name" value="操作動畫" id="btn" />
</body>

動畫方法

6.多行文本框的應(yīng)用-高度變化

復(fù)制代碼 代碼如下:
<script src="script/jquery-1.7.1.min.js"></script>
<style>
input:focus,textarea:focus {
border:1px solid #f00;
 
}
</style>
<script>
$(function () {
var comment = $("#comment");
$(".bigger").click(function () {
if (comment.height() < 500) {
comment.height($("#comment").height() + 100);  //在原有高度的基礎(chǔ)上增高100
}
});
$(".smaller").click(function () { 
if (comment.height() > 100) {
comment.height($("#comment").height() - 100);  //在原有高度的基礎(chǔ)上降低100
}
});
})
</script>
<body>
<form action="#" method="post" id="regform">
<div class="msg"><span class="bigger">放大</span><span class="smaller">縮小</span></div>
<div style="" data-mce-style="color: #800000;">"><textarea rows="8" cols="20" id="comment">海海海海</textarea></div>
</form>
</body>

上面的操作實現(xiàn)了點擊放大時間,textarea的高度變高即面積變大,當(dāng)點擊縮小時間textarea的面積變小,即實現(xiàn)了動畫的效果。

7.復(fù)選框應(yīng)用

復(fù)制代碼 代碼如下:
<script src="script/jquery-1.7.1.min.js"></script>
<script>
$(function () {
$("#checkall").bind("click", function () {
$(":checkbox").each(function () {
$(this).attr("checked", "checked");         //點擊按鈕時間需要全部選中
});
});
$("#checkno").bind("click", function () {
$(":checkbox").attr("checked", false);    //點擊按鈕時間需要全部不選中
});
$("#checkRev").bind("click", function () {
$(":checkbox").each(function () {
if ($(this).attr("checked") == "checked") {
$(this).attr("checked", false);
}
else {
$(this).attr("checked", true);   //點擊按鈕時間需要選中的清除,未選中的被選中
}
});
});
//或者:
$(this).attr("checked", !$(this).attr("checked"));
});
</script>
<body>
<form>你愛好的運(yùn)動?<br />
<input type="checkbox" name="names" value="足球 "  />足球<br />
<input type="checkbox" name="names" value="籃球 " />籃球<br />
<input type="checkbox" name="names" value="排球 " />排球<br />
<input type="checkbox" name="names" value="羽毛球 " />羽毛球<br />
<input type="button" id="checkall" value="全選 " /><br />
<input type="button" id="checkno" value="全不選 " /><br />
<input type="button" id="checkRev" value="反選 " /><br />
<input type="button" name="send" value="提交" /><br />
</form>
</body>

在這里需要注意的是,判斷復(fù)選框選中或者不選中的狀態(tài),必須通過控制元素的checked屬性來達(dá)到目的,如果屬性checked為true,說明被選中,如果為false,則說明未被選中。

8.下拉框的應(yīng)用

復(fù)制代碼 代碼如下:
<script src="script/jquery-1.7.1.min.js"></script>
<script>
$(function () {
$("#add").click(function () {
var selectoption = $("#select1 option:selected");
selectoption.remove();
selectoption.appendTo('#select2');    //把選中的項添加到右邊的aelect框中
});
$("#addAll").bind("click",function () {
var options = $("#select1 option");
options.appendTo('#select2');
});
});
</script>
<body>
<div class="center">
<select multiple="multiple" id="select1" style="width: 100px; height: 160px">
<option value="1">選項1</option><option value="2">選項2</option> <option value="3">選項3</option>
<option value="4">選項4</option><option value="5">選項5</option><option value="6">選項6</option>
<option value="7">選項7</option><option value="8">選項8</option><option value="9">選項9</option>
</select>
<div>
<span id="add">添加到右邊</span>
<span id="addAll">全部添加到右邊</span>
</div>
</div>
<div class="center" style="float:right">
<select multiple="multiple" id="select2" style="width: 100px; height: 160px" >
</select>
</div>

上面的操作是實現(xiàn)了在在左邊點擊選擇的項,然后添加到右邊的框中,可以一個一個的添加,也可以全部一次性添加。

9.表格的應(yīng)用

復(fù)制代碼 代碼如下:
<script src="script/jquery-1.7.1.min.js"></script>
<style>
.even {
 
}
.odd {
background-color: #ffffee;
}
</style>
<script>
$("#table tr:odd").addClass("odd");          //選取索引為奇數(shù)的行數(shù)
$("#table tr:even:not(:first)").addClass("even");   //選取索引為偶數(shù)的除了索引為0的行數(shù)
$("table tr").each(function () {
$(this).click(function () {
$(this).css("backgroundColor","red").siblings().css("backgroundColor","");
});
})
</script>
<body>
<table border="1" id="table">
<thead><tr><th>姓名</th><th>性別</th><th>暫住地</th></tr></thead><tbody>
<tr class="parent" id="row1"><td colspan="3">前臺設(shè)計組</td></tr>
<tr class="child1"><td>張三</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child1"><td>李四</td><td>女</td><td>浙江杭州</td></tr>
<tr class="parent" id="row2"><td colspan="3">前臺開發(fā)組</td></tr>
<tr class="child2"><td>王五</td><td>男</td><td>湖南長沙</td></tr>
<tr class="child2"><td>趙六</td><td>男</td><td>湖南長沙</td></tr>
<tr class="parent" id="row3"><td colspan="3">后臺開發(fā)組</td></tr>
<tr class="child3"><td>孫七</td><td>男</td><td>湖南長沙</td></tr>
<tr class="child3"><td>周八</td><td>男</td><td>湖南長沙</td>
</tr>
</tbody>
</table>
</body>

希望本文所述對大家的jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論