猿问

$(this)和event.target之间的区别?

$(this)和event.target之间的区别?

我是jQuery的新手,正在制作选项卡式面板,遵循JavaScript和jQuery教程:The Missing Manual,当作者这样做时,第一行是:

   var target = $(this);

但我试着这样做

   var target = evt.target;

我得到了那个错误:

Uncaught TypeError: Object http://localhost/tabbedPanels/#panel1 has no method '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>
慕桂英3389331
浏览 950回答 3
3回答

慕尼黑的夜晚无繁华

jQuery如何使用“on”方法处理此变量有很大的不同$("outer&nbsp;DOM&nbsp;element").on('click',"inner&nbsp;DOM&nbsp;element",function(){ &nbsp;&nbsp;$(this)&nbsp;//&nbsp;refers&nbsp;to&nbsp;the&nbsp;"inner&nbsp;DOM&nbsp;element"})如果你将它与: -$("outer&nbsp;DOM&nbsp;element").click(function(){ &nbsp;&nbsp;$(this)&nbsp;//&nbsp;refers&nbsp;to&nbsp;the&nbsp;"outer&nbsp;DOM&nbsp;element"})
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答