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

jquery 關于event.target使用的幾點說明介紹

 更新時間:2013年04月26日 14:33:33   作者:  
本篇文章介紹了,jquery中關于event.target使用的幾點說明。需要的朋友參考下

event.target
說明:引發(fā)事件的DOM元素。

this和event.target的區(qū)別
js中事件是會冒泡的,所以this是可以變化的,但event.target不會變化,它永遠是直接接受事件的目標DOM元素;

this和event.target的相同點
this和event.target都是dom對象,如果要使用jquey中的方法可以將他們轉(zhuǎn)換為jquery對象:$(this)和$(event.target);

這使我想起了以前寫的一個例子:

復制代碼 代碼如下:

    //del event
    $(".del").bind("click",function(event){
        var _tmpQuery=$(this);//為什么要加上這一句?
        var id=$("input[name='id']",$(this).parents("form:first")).attr("value");
        art.dialog.confirm('你確認刪除該日志嗎?',function(){
            $.post("myRun/managerlog_del.php",{id:id},function(tips){
                if(tips=='ok'){
                    art.dialog.tips('成功刪除');
                    $(_tmpQuery.parents('tr:first')).hide();//如果不加第一句,這里用$($(this).parents('tr:first')).hide();則不會隱藏
                    //因為這里的this,并不是當前的class="del"這個DOM對象了。而是jQuery的AJAX配置對象ajaxSettings。測試:alert(this.url);
                }else{
                    art.dialog.tips(tips,5);
                }
            });
            return true;
        });
    });

那么現(xiàn)在我可以將上面代碼通過$(event.target)這個方式來實現(xiàn)隱藏tr,而不用通過$(_tmpQuery.parents('tr:first')).hide();這樣的方式,具體代碼如下:
復制代碼 代碼如下:

$(".del").bind("click",function(event){
    //var _tmpQuery=$(this);這行代碼可以刪除
    var id=$("input[name='id']",$(this).parents("form:first")).attr("value");
    art.dialog.confirm('你確認刪除該日志嗎?',function(){
        $.post("myRun/managerlog_del.php",{id:id},function(tips){
            if(tips=='ok'){
                art.dialog.tips('成功刪除');
                $(event.target).parents('tr:first').hide();
            }else{
                art.dialog.tips(tips,5);
            }
        });
        return true;
    });
});

event.target和$(event.target)的使用
復制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script language="JavaScript" type="text/JavaScript" src="http://www.w3school.com.cn/jquery/jquery.js"></script>
<script type="text/javascript">
$(function(){
&nbsp; &nbsp; $("li").live("click",function(event){
&nbsp; &nbsp; &nbsp; &nbsp; $("#temp").html("clicked: " + event.target.nodeName);
&nbsp; &nbsp; &nbsp; &nbsp; $(event.target).css("color","#FF3300");
&nbsp; &nbsp; })
});
</script>
</head>


<body>
&nbsp; &nbsp; <div id="temp"></div>
&nbsp; &nbsp; <ul class="JQ-content-box" style="padding:20px; background:#FFFFFF">
&nbsp; &nbsp; &nbsp; &nbsp; <li>第一行
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>這是公告標題1</li>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>這是公告標題2</li>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>這是公告標題3</li>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>這是公告標題4</li>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>
&nbsp; &nbsp; &nbsp; &nbsp; </li>
&nbsp; &nbsp; </ul>
</body>
</html>


上面的例子如果改成使用this
復制代碼 代碼如下:

<script type="text/javascript">
$(function(){
    $("li").live("click",function(event){
        $("#temp").html("clicked: " + event.target.nodeName);
        $(this).css("color","#FF3300");
        event.stopPropagation();
    })
});
</script>

在看一個例子
復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>

<script language="JavaScript" type="text/JavaScript" src="http://www.w3school.com.cn/jquery/jquery.js"></script>
<script>
$(document).ready(function(){
    function handler(event) {
      var $target = $(event.target);
      if( $target.is("li") ) {
        $target.children().toggle();
      }
    }
    $("ul").click(handler).find("ul").hide();//從這里也看出find只在后代中遍歷,不包括自己。
});
</script>
</head>
<body>

<ul>
  <li>item 1
    <ul>
      <li>sub item 1-a</li>
      <li>sub item 1-b</li>
    </ul>
  </li>
  <li>item 2
    <ul>
      <li>sub item 2-a</li>
      <li>sub item 2-b</li>
    </ul>
  </li>
</ul>

</body>
</html>


toggle()不帶參數(shù)的作用:

toggle有兩種作用:
toggle()
切換元素的可見狀態(tài)。
如果元素是可見的,切換為隱藏的;如果元素是隱藏的,切換為可見的。

toggle(fn,fn)
每次點擊時切換要調(diào)用的函數(shù)。
如果點擊了一個匹配的元素,則觸發(fā)指定的第一個函數(shù),當再次點擊同一元素時,則觸發(fā)指定的第二個函數(shù)。隨后的每次點擊都重復對這兩個函數(shù)的輪番調(diào)用。
可以使用unbind("click")來刪除。

相關文章

最新評論