1、頁面加載時觸發ready()
$(document).ready(function(){})等價於$(function(){});
<body>
<h3>頁面載入時觸發ready()事件</h3>
<div id="tip"></div>
<input id="btntest" type="button" value="點下我" />
<script type="text/javascript">
$(document).ready(function(){
$("#btntest").bind("click", function () {
$("#tip").html("我被點擊了!");
});
});
</script>
</body>
2、bind()方法綁定元素的事件,綁定格式如下:
$(selector).bind(event,[data] function) ,若多個event用空格分隔
<body>
<h3>bind()方法綁多個事件</h3>
<input id="btntest" type="button" value="點擊或移出就不可用了" />
<script type="text/javascript">
$(function () {
$("#btntest").bind("click", function () {
$(this).attr("disabled", "true");
})
});
</script>
</body>
3、hover()方法切換事件
hover()方法的功能是當鼠標移到所選元素上時,執行方法中的第一個函數,鼠標移出時,執行方法中的第二個函數,調用格式如下:
$(selector).hover(over,out);
<body>
<h3>hover()方法切換事件</h3>
<div>別走!你就是土豪</div>
<script type="text/javascript">
$(function () {
$("div").hover(
function () {
$(this).addClass("orange");
},
function () {
$(this).removeClass("orange")
})
});
</script>
</body>
div
{
width: 160px;
border: solid 1px #ccc;
padding:8px;
text-align:center;
}
.orange
{
background-color: Orange;
color:White;
}
4、toggle()方法可實現元素隱藏或點擊顯示不同事件
toggle()方法可以在元素的click事件中綁定兩個或兩個以上的函數,同時,它還可以實現元素的隱藏與顯示的切換,綁定多個函數的調用格式如下:
$(selector).toggle(fun1(),fun2(),funN(),...)
其中,fun1,fun2就是多個函數的名稱
<body>
<h3>toggle()方法綁定多個函數</h3>
<input id="btntest" type="button" value="點一下我" />
<div>我是動態顯示的</div>
<script type="text/javascript">
$(function () {
$("#btntest").bind("click", function () {
$("div").toggle()
})
});
</script>
</body>
5、unbind()移除已有樣式
unbind()方法可以移除元素已綁定的事件,它的調用格式如下:
$(selector).unbind(event,fun)
其中參數event表示需要移除的事件名稱,多個事件名用空格隔開,fun參數爲事件執行時調用的函數名稱。unbind()方法,如是方法中沒有任何參數,則表示移除全部已綁定的事件。
<body>
<h3>unbind()移除綁定的事件</h3>
<input id="btntest" type="button" value="移除事件" />
<div>土豪,咱們交個朋友吧</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>
6、one()綁定一次性事件
one()方法可以綁定元素任何有效的事件,但這種方法綁定的事件只會觸發一次,它的調用格式如下:
$(selector).one(event,[data],fun)
參數event爲事件名稱,data爲觸發事件時攜帶的數據,fun爲觸發該事件時執行的函數。
<body>
<h3>one()方法執行一次綁定事件</h3>
<div>請點擊我一下</div>
<script type="text/javascript">
$(function () {
var intI = 0;
$("div").one("click", function () {
intI++;
$(this).css("font-size", intI + "px");
})
});
</script>
</body>
7、trigger(),頁面加載完成即觸發事件
trigger()方法可以直接手動觸發元素指定的事件它的調用格式爲:
$(selector).trigger(event)
其中event參數爲需要手動觸發的事件名稱。
8、focus,blur對文本框的是否爲空顯示不同內容
focus事件在元素獲取焦點時觸發,如點擊文本框時,觸發該事件;而blur事件則在元素丟失焦點時觸發,如點擊除文本框的任何元素,都會觸發該事件。
<body>
<h3>表單中文本框的focus和blur事件</h3>
<input id="txtest" type="text" value="" />
<div></div>
<script type="text/javascript">
$(function () {
$("input")
.bind("focus", function () {
$("div").html("請輸入您的姓名!");
})
.bind("blur", function () {
if ($(this).val().length == 0)
$("div").html("你的名稱不能爲空!");
})
});
</script>
</body>
9、change() 可改變下拉框選中事件
當一個元素的值發生變化時,將會觸發change事件,例如在選擇下拉列表框中的選項時,就會觸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>
10、live()類似bind()也可綁定元素
與bind()方法相同,live()方法與可以綁定元素的可執行事件,除此之外,live()方法還可以綁定動態元素,即使用代碼添加的元素事件,格式如下:
$(selector).live(event,[data],fun)
參數event爲事件名稱,data爲觸發事件時攜帶的數據,fun爲觸發該事件時執行的函數。