我是jQuery的新手,我正在制作选项卡式面板,按照JavaScript and jQuery : The Missing Manual中的教程,作者这样做时有第一行:

   var target = $(this);

But I tried to do it that way

   var target = evt.target;

and I got that error :

未捕获的类型错误:对象http://localhost/tabbedPanels/#panel1没有方法"Attr"

当我把evt.target改回$(this)的时候,它就像一个护身符一样起作用了.

我想知道$(this)evt.target的区别是什么?

这是我的代码,以防你需要它:

index.html :

<!DOCTYPE html>
<html>
    <head>
        <title>Tabbed Panel</title>
        <style>
            body {
               width : 100%;
               height: 100%;
            }
            
            #wrapper {
                margin : auto;
                width : 800px;                
            }
            
            #tabsContainer {
                overflow: hidden;
            }
            
            #tabs {                
                padding:0;
                margin:0;
            }                
            
            #tabs li {
                float : left;
                list-style:none;
            }
            
            #tabs a {
                text-decoration:none;
                padding : 3px 5px;                
                display : block;                
            }
            
            #tabs a.active {
                background-color : grey;                
            }            
            #panelsContainer {
                clear: left;
            }            
            #panel1 {
                color : blue;
            }            
            #panel2 {
                color : yellow;
            }
            #panel3 {
                color: green;
            }
            #panel4 {
                color : black;
            }         
            
        </style>
        <script type="text/javascript" src="jquery-1.8.0.min.js"></script>
        <script type="text/javascript" src="script.js"></script>        
    </head>
    
    <body>
        <div id="wrapper">
            <div id="tabsContainer">
                <ul id="tabs">
                    <li><a href="#panel1">Panel1</a></li>
                    <li><a href="#panel2">Panel2</a></li>
                    <li><a href="#panel3">Panel3</a></li>
                    <li><a href="#panel4">Panel4</a></li>
                </ul>
            </div>
            <div id="panelsContainer">
                <div id="panel1" class="panel">
                    this is panel1
                </div>
                <div id="panel2" class="panel">
                    this is panel2
                </div>
                <div id="panel3" class="panel">
                    this is panel3
                </div>
                <div id="panel4" class="panel">
                    this is panel4
                </div>                
            </div>
        </div>
        
    </body>
    
</html>

script.js :

$(function(){
    $("#tabs a").click(function(evt){
       var target = evt.target,
           targetPanel = target.attr("href");
       $(".panel").hide();
       $("#tabs a.active").removeClass("active");
       target.addClass("active").blur();
       $(targetPanel).fadeIn(300);
       evt.preventDefault();
    });
    
    $("#tabs a:first").click();
})

推荐答案

$(this)event.target之间有一个很大的差别.this(或event.currentTarget,见下文)总是指侦听器附加到的DOM元素,而event.target是实际单击的DOM元素.记住,由于事件冒泡,如果您有

<div class="outer">
  <div class="inner"></div>
</div>

并将Click Listener附加到外部div

$('.outer').click( handler );

then the handler will be invoked when you click inside the outer div as well as the inner one (unless you have other code that handles the event on the inner div and stops propagation).

In this example, when you click inside the inner div, then in the handler:

  • this引用.outerDOM元素(因为它是处理程序附加到的对象)
  • event.currentTarget还引用.outer元素(因为它是处理事件的current target元素)
  • event.target表示.inner元素(这会给出事件发生的元素)

The jQuery wrapper $(this) only wraps the DOM element in a jQuery object so you can call jQuery functions on it. You can do the same with $(event.target).

还要注意,如果您重新绑定this的上下文(例如,如果您使用Backbone,它将自动完成),它将指向其他内容.您总是可以从event.currentTarget获得实际的DOM元素.

Jquery相关问答推荐

jQuery 时间 Select 器

未捕获的类型错误:无法读取未定义的属性toLowerCase

AngularJS 中的 ScrollTo 函数

在 for 循环中分配点击处理程序

JSON字符串到JS对象

非 AJAX jQuery POST 请求

动态设置 iframe src

字符串化(转换为 JSON)具有循环引用的 JavaScript 对象

使用jquery设置di​​v标签的值

使用带有 HTML 表格的 jQuery UI 可排序

使用 jQuery 按字母顺序对选项元素进行排序

jQuery ajax (jsonp) 忽略超时并且不触发错误事件

JQuery:如果 div 可见

我如何知道 jQuery 是否有待处理的 Ajax 请求?

如何使用 javascript 擦除所有内联样式并仅保留 css 样式表中指定的样式?

如果一个元素正在被动画,我如何用 jQuery 找出?

我可以通过 JavaScript 禁用 CSS :hover 效果吗?

JavaScript 中的简单节流阀

jQuery.active 函数

获取对象属性中的最小值/最大值的快速方法