jquery 焦點(diǎn)新聞-js獲取焦點(diǎn)事件
1、jQuery的事件有哪些?
javscript的事件:onClick,onmouseover
jquery的事件:click,mouseover
注意jQuery的事件比javascript的事件少一個(gè)on
1)鼠標(biāo)事件
click:單擊事件
//dbclick:雙擊事件
mouseover:鼠標(biāo)懸念(移入)
mouseout:鼠標(biāo)移出
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
<style>
.abc{
background-color: #aaa;
}
</style>
<script type="text/javascript">
$(function(){
$("#nav li").mouseover(function(){
$(this).addClass("abc");//當(dāng)鼠標(biāo)移入li元素時(shí)添加樣式
}).mouseout(function(){
//$(this).removeClass() //當(dāng)鼠標(biāo)移出時(shí)刪除所有樣式
$(this).removeClass("abc") //當(dāng)鼠標(biāo)移出時(shí)刪除指定樣式
});
});
</script>
<div id="nav">
<ul>
<li><a href="#">首頁</a></li>
<li><a href="#">服裝</a></li>
<li><a href="#">電器</a></li>
<li><a href="#">團(tuán)購</a></li>
<li><a href="#">在線瀏覽</a></li>
</ul>
</div>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
2) 鍵盤事件
keydown:鍵盤按下事件
keypress:鍵盤按下并放開時(shí)觸發(fā)
keydown:鍵盤放開時(shí)觸發(fā)
- 1
- 2
- 3
- 4
- 5
<script type="text/javascript">
$(function(){
//按回車鍵自動(dòng)登錄(不用再點(diǎn)擊登錄按鈕)
$(document).keydown(function(event){
if(event.keyCode==13){
$("#actionForm").submit();
return true;
}
return false;
})
});
</script>
<form id="actionForm" action="../index.html">
登錄名:<input id="txtLoginName" /><br />
密碼:<input type="password" />
<input type="button" value="登錄" />
</form>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3) 表單事件
focus:獲得焦點(diǎn)時(shí)觸發(fā)
blur:失去焦點(diǎn)
select:文本選中時(shí)觸發(fā)
- 1
- 2
- 3
- 4
- 5
<script type="text/javascript">
$(function(){
//文本框獲得焦點(diǎn)時(shí),將當(dāng)前文本框的內(nèi)容清空(值等于"請(qǐng)輸入登錄名..")
//文本框失去焦點(diǎn)時(shí),判斷內(nèi)容是否為"請(qǐng)輸入登錄名.."或是否為空,那么將這個(gè)值"請(qǐng)輸入登錄名.."設(shè)回到文本框
$("#txtLoginName").focus(function(){
if($(this).val()=='請(qǐng)輸入登錄名...'){
$(this).val('');//清空內(nèi)容
}
}).blur(function(){
if($(this).val().trim()==''){
$(this).val('請(qǐng)輸入登錄名...');//清空內(nèi)容
}
});
});
</script>
<form id="actionForm" action="../index.html">
登錄名:<input id="txtLoginName" value="請(qǐng)輸入登錄名..." /><br />
密碼:<input type="password" />
<input type="button" value="登錄" />
</form>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
4)其它事件
bind():綁定事件
unbind():移除綁定事件
- 1
- 2
- 3
- 4
<script type="text/javascript">
$(function(){
//綁定單個(gè)事件
// $("#btn1").bind('click',function(){
// alert("OK");
// });
//綁定多個(gè)事件
$("#btn1").bind({
mouseover:function(){
alert("鼠標(biāo)懸念");
},
mouseout:function(){
alert("鼠標(biāo)移出");
}
})
//解除綁定事件
$("#btn2").click(function(){
//$("#btn1").unbind("click");//解除綁定的單個(gè)事件
$("#btn1").unbind("mouseover mouseout");//解除多個(gè)綁定的事件,用空格分隔
//$("#btn1").unbind();//解除所有綁定的事件
});
});
</script>
<input type="button" id="btn1" value="點(diǎn)我" /><br />
<input type="button" id="btn2" value="解除綁定" />
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
hover():相當(dāng)于mouseover和mouseout的組合
- 1
- 2
.abc{
background-color: #aaa;
}
</style>
<script type="text/javascript" src="../js/jquery-1.8.3.js" ></script>
<script type="text/javascript">
$(function(){
// $("#nav li").mouseover(function(){
// $(this).addClass("abc");//當(dāng)鼠標(biāo)移入li元素時(shí)添加樣式
// }).mouseout(function(){
// //$(this).removeClass() //當(dāng)鼠標(biāo)移出時(shí)刪除所有樣式
// $(this).removeClass("abc") //當(dāng)鼠標(biāo)移出時(shí)刪除指定樣式
// });
$("#nav li").hover(
function(){
$(this).addClass("abc");//當(dāng)鼠標(biāo)移入li元素時(shí)添加樣式
},
function(){
$(this).removeClass("abc") //當(dāng)鼠標(biāo)移出時(shí)刪除指定樣式
}
);
});
</script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
togger():
a) 鼠標(biāo)連續(xù)點(diǎn)擊
b) 顯示和隱藏
- 1
- 2
- 3
- 4
<script type="text/javascript">
$(function(){
//點(diǎn)擊當(dāng)前頁面時(shí)顯示紅綠藍(lán)的背景色
$("body").toggle(
function(){
$(this).css("background-color","red");
},
function(){
$(this).css("background-color","green");
},
function(){
$(this).css("background-color","blue");
}
);
});
</script>
</head>
<body>
測(cè)試
</body>


