jquery 焦點新聞-js獲取焦點事件

首頁 > 焦點 > 今日關注 > 正文

jquery 焦點新聞-js獲取焦點事件

1、jQuery的事件有哪些?

javscript的事件:onClick,onmouseover

jquery的事件:click,mouseover

注意jQuery的事件比javascript的事件少一個on

1)鼠標事件

click:單擊事件

//dbclick:雙擊事件

mouseover:鼠標懸念(移入)

mouseout:鼠標移出

  • 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");//當鼠標移入li元素時添加樣式

}).mouseout(function(){

//$(this).removeClass() //當鼠標移出時刪除所有樣式

$(this).removeClass("abc") //當鼠標移出時刪除指定樣式

});

});

</script>

<div id="nav">

<ul>

<li><a href="#">首頁</a></li>

<li><a href="#">服裝</a></li>

<li><a href="#">電器</a></li>

<li><a href="#">團購</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:鍵盤按下并放開時觸發

keydown:鍵盤放開時觸發

  • 1
  • 2
  • 3
  • 4
  • 5

<script type="text/javascript">

$(function(){

//按回車鍵自動登錄(不用再點擊登錄按鈕)

$(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:獲得焦點時觸發

blur:失去焦點

select:文本選中時觸發

  • 1
  • 2
  • 3
  • 4
  • 5

<script type="text/javascript">

$(function(){

//文本框獲得焦點時,將當前文本框的內容清空(值等于"請輸入登錄名..")

//文本框失去焦點時,判斷內容是否為"請輸入登錄名.."或是否為空,那么將這個值"請輸入登錄名.."設回到文本框

$("#txtLoginName").focus(function(){

if($(this).val()=='請輸入登錄名...'){

$(this).val('');//清空內容

}

}).blur(function(){

if($(this).val().trim()==''){

$(this).val('請輸入登錄名...');//清空內容

}

});

});

</script>

<form id="actionForm" action="../index.html">

登錄名:<input id="txtLoginName" value="請輸入登錄名..." /><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(){

//綁定單個事件

// $("#btn1").bind('click',function(){

// alert("OK");

// });

//綁定多個事件

$("#btn1").bind({

mouseover:function(){

alert("鼠標懸念");

},

mouseout:function(){

alert("鼠標移出");

}

})

//解除綁定事件

$("#btn2").click(function(){

//$("#btn1").unbind("click");//解除綁定的單個事件

$("#btn1").unbind("mouseover mouseout");//解除多個綁定的事件,用空格分隔

//$("#btn1").unbind();//解除所有綁定的事件

});

});

</script>

<input type="button" id="btn1" value="點我" /><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():相當于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");//當鼠標移入li元素時添加樣式

// }).mouseout(function(){

// //$(this).removeClass() //當鼠標移出時刪除所有樣式

// $(this).removeClass("abc") //當鼠標移出時刪除指定樣式

// });

$("#nav li").hover(

function(){

$(this).addClass("abc");//當鼠標移入li元素時添加樣式

},

function(){

$(this).removeClass("abc") //當鼠標移出時刪除指定樣式

}

);

});

</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) 鼠標連續點擊

b) 顯示和隱藏

  • 1
  • 2
  • 3
  • 4

<script type="text/javascript">

$(function(){

//點擊當前頁面時顯示紅綠藍的背景色

$("body").toggle(

function(){

$(this).css("background-color","red");

},

function(){

$(this).css("background-color","green");

},

function(){

$(this).css("background-color","blue");

}

);

});

</script>

</head>

<body>

測試

</body>

jquery 焦點新聞-js獲取焦點事件

備案號:贛ICP備2022005379號
華網(http://www.fshsdq.com.cn) 版權所有未經同意不得復制或鏡像

QQ:51985809郵箱:51985809@qq.com