hi
睡得恍恍惚惚不知精神為何物了
1、jQuery
-----事件與應(yīng)用-----
----頁面加載時(shí)觸發(fā)ready()事件
ready()
事件類似于onLoad()
事件,但前者只要頁面的DOM結(jié)構(gòu)加載后便觸發(fā),而后者必須在頁面全部元素加載成功才觸發(fā),ready()
可以寫多個(gè),按順序執(zhí)行。此外,下列寫法是相等的:
$(document).ready(function(){})
等價(jià)于$(function(){});
<body>
<h3>頁面載入時(shí)觸發(fā)ready()事件</h3>
<div id="t <input id="btntest" type="button" value="點(diǎn)下我" />
<script type="text/javascript">
$("#btntest").ready(function()) {
$("#btntest").bind("click", function () {
$("#tip").html("我被點(diǎn)擊了!");
});
});
</script>
</body>
----使用bind()方法綁定元素的事件
bind()
方法綁定元素的事件非常方便,綁定前,需要知道被綁定的元素名,綁定的事件名稱,事件中執(zhí)行的函數(shù)內(nèi)容就可以,它的綁定格式如下:
$(selector).bind(event,[data] function)
參數(shù)event為事件名稱,多個(gè)事件名稱用空格隔開,function為事件執(zhí)行的函數(shù)。
<body>
<h3>bind()方法綁多個(gè)事件</h3>
<input id="btntest" type="button" value="點(diǎn)擊或移出就不可用了" />
<script type="text/Javascript">
$(function () {
$("#btntest").bind("click mouSEOut" , function () {
$(this).attr("disabled", "true");
})
});
</script>
</body>
----使用hover()方法切換事件
hover()
方法的功能是當(dāng)鼠標(biāo)移到所選元素上時(shí),執(zhí)行方法中的第一個(gè)函數(shù),鼠標(biāo)移出時(shí),執(zhí)行方法中的第二個(gè)函數(shù),實(shí)現(xiàn)事件的切實(shí)效果,調(diào)用格式如下:
$(selector).hover(over,out);
over參數(shù)為移到所選元素上觸發(fā)的函數(shù),out參數(shù)為移出元素時(shí)觸發(fā)的函數(shù)。
<body>
<h3>hover()方法切換事件</h3>
<div>別走!你就是土豪</div>
<script type="text/javascript">
$(function () {
$("div").hover(
function () {
$(this).addClass("orange");
},
function () {
$(this).removeClass("orange")
})
});
</script>
</body>
----使用toggle()方法綁定多個(gè)函數(shù)
toggle()
方法可以在元素的click事件中綁定兩個(gè)或兩個(gè)以上的函數(shù),同時(shí),它還可以實(shí)現(xiàn)元素的隱藏與顯示的切換,綁定多個(gè)函數(shù)的調(diào)用格式如下:
$(selector).toggle(fun1(),fun2(),funN(),...)
其中,fun1,fun2就是多個(gè)函數(shù)的名稱
<body>
<h3>toggle()方法綁定多個(gè)函數(shù)</h3>
<input id="btntest" type="button" value="點(diǎn)一下我" />
<div>我是動(dòng)態(tài)顯示的</div>
<script type="text/javascript">
$(function () {
$("#btntest").bind("click", function () {
$("div").toggle();}
</script>
</body>
----使用unbind()方法移除元素綁定的事件
unbind()
方法可以移除元素已綁定的事件,它的調(diào)用格式如下:
$(selector).unbind(event,fun)
其中參數(shù)event表示需要移除的事件名稱,多個(gè)事件名用空格隔開,fun參數(shù)為事件執(zhí)行時(shí)調(diào)用的函數(shù)名稱。
如果沒有規(guī)定參數(shù),unbind() 方法會(huì)刪除指定元素的所有事件處理程序。
<body>
<h3>unbind()移除綁定的事件</h3>
<input id="btntest" type="button" value="移除事件" />
<div>土豪,咱們交個(gè)朋友吧</div>
<script type="text/javascript">
$(function () {
$("div").bind("click",
function () {
$(this).removeClass("backcolor").addClass("color");
}).bind("dblclick", function () {
$(this).removeClass("color").addClass("backcolor");
})
$("#btntest").bind("click", function () {
$("div").unbind()
$(this).attr("disabled", "true");
});
});
</script>
</body>
----使用one()方法綁定元素的一次性事件
one()
方法可以綁定元素任何有效的事件,但這種方法綁定的事件只會(huì)觸發(fā)一次,它的調(diào)用格式如下:
$(selector).one(event,[data],fun)
參數(shù)event為事件名稱,data為觸發(fā)事件時(shí)攜帶的數(shù)據(jù),fun為觸發(fā)該事件時(shí)執(zhí)行的函數(shù)。
<body>
<h3>one()方法執(zhí)行一次綁定事件</h3>
<div>請點(diǎn)擊我一下</div>
<script type="text/javascript">
$(function () {
var intI = 0;
$("div").one("click", function () {
intI++;
$(this).CSS("font-size", intI + "px");
})
});
</script>
</body>
----調(diào)用trigger()方法手動(dòng)觸發(fā)指定的事件
trigger()
方法可以直接手動(dòng)觸發(fā)元素指定的事件,這些事件可以是元素自帶事件,也可以是自定義的事件,總之,該事件必須能執(zhí)行,它的調(diào)用格式為:
$(selector).trigger(event)
其中event參數(shù)為需要手動(dòng)觸發(fā)的事件名稱。
<body>
<h3>trigger()手動(dòng)觸發(fā)事件</h3>
<div>土豪,咱們交個(gè)朋友吧</div>
<script type="text/javascript">
$(function () {
$("div").bind("change-color", function () {
$(this).addClass("color");
});
$("div").trigger("change-color");
});
</script>
</body>
----文本框的focus和blur事件
focus事件在元素獲取焦點(diǎn)時(shí)觸發(fā),如點(diǎn)擊文本框時(shí),觸發(fā)該事件;而blur事件則在元素丟失焦點(diǎn)時(shí)觸發(fā),如點(diǎn)擊除文本框的任何元素,都會(huì)觸發(fā)該事件。
<body>
<h3>表單中文本框的focus和blur事件</h3>
<input id="txtest" type="text" value="" />
<div></div>
<script type="text/javascript">
$(function () {
$("input")
.bind("focus", function () {
$("div").html("請輸入您的姓名!");
})
$("input").bind("blur", function () {
if ($(this).val().length == 0)
$("div").html("你的名稱不能為空!");
})
});
</script>
</body>
----下拉列表框的change事件
當(dāng)一個(gè)元素的值發(fā)生變化時(shí),將會(huì)觸發(fā)change
事件,例如在選擇下拉列表框中的選項(xiàng)時(shí),就會(huì)觸change
事件。
<body>
<h3>下拉列表的change事件</h3>
<select id="seltest">
<option value="葡萄">葡萄</option>
<option value="蘋果">蘋果</option>
<option value="荔枝">荔枝</option>
<option value="香焦">香焦</option>
</select>
<script type="text/javascript">
$(function () {
$("select").bind("change", function () {
if ($(this).val() == "蘋果")
$(this).css("background-color", "red");
else
$(this).css("background-color", "green");
})
});
</script>
</body>
----調(diào)用live()方法綁定元素的事件
與bind()
方法相同,live()
方法與可以綁定元素的可執(zhí)行事件,除此相同功能之外,live()
方法還可以綁定動(dòng)態(tài)元素,即使用代碼添加的元素事件,格式如下:
$(selector).live(event,[data],fun)
參數(shù)event為事件名稱,data為觸發(fā)事件時(shí)攜帶的數(shù)據(jù),fun為觸發(fā)該事件時(shí)執(zhí)行的函數(shù)。
<body>
<h3>live()方法綁多個(gè)事件</h3>
<script type="text/javascript">
$(function () {
$("#btntest").live("click mouseout", function () {
$(this).attr("disabled", "true");
})
$("body").append("<input id='btntest' type='button' value='點(diǎn)擊或移出就不可用了' />");
});
</script>
</body>
----
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注