当前位置:Web前端 > jquery> 正文

jQuery end()

时间:2015-6-6类别:Web前端

jQuery end()

jQuery end()

end() 方法结束当前链条中的最近的筛选操作,并将匹配元素集还原为之前的状态。

 

一、jQuery()中end()的源码

  •  
  • 
        // 所有通过pushStack方法获得的jQuery对象都可以通过end方法返回之前的状态
        // 如果有preObject对象这返回该对象,否则调用构造函数返回jQuery的空对象
         end: function() {
             return this.prevObject || this.constructor(null);
         },
    
    		
  •  
  •  
  •  
  •  
  • 二、jQuery()中end()实例
  •  
  •  
  • 
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    
    <body>
      <p><span>Hello</span>, how are you?</p>
      <script>$("p").find("span").end().css("border", "2px red solid");</script>
    </body>
    </html>
    
    //说明
    
    //$("p").find("span")表示查找P元素下的SPAN元素
    
    //但是我想更改P的边框,这时我就要返回到P元素(即从SPAN返回到P,就是还原为之前的状态)
    
    //$("p").find("span").end()这个语句就返回来了。
    
    //$("p").find("span").end().css("border", "2px red solid")把P的边框设置了。
    
    				
  •  

  • 
    
    <li id="test">  
        <h1>jQuery end()方法</h1>  
        <p>讲解jQuery中end()方法。</p>  
    </li> 
    
    <scripg>
    $(document).ready(function() ...{   
        $("#test").click(function() ...{   
            $(this).find("p").hide().end().hide();   
        });   
    });
    </script>
    
    
    
    			//说明  //点击id为test的li时,首先找到li里边的p标签,将其隐藏。 //接下来使用end()方法结束了对p标签的引用,此时返回的是#test(jQuery对象),从而后边的hide()方法隐藏了li。  
  •  

  • 
    <script type="text/javascript">
        $(function(){
            $('<input type="button" value="click me" /><input type="button" value="triggle click me" /><input type="button" value="detach handle" /><input type="button" value="show/hide text" />').appendTo($('body'));
            $('input[type="button"]').eq(0).click(function(){
                alert('you clicked me!');
            })
            .end().eq(1).click(function(){
                $('input[type="button"]:eq(0)').trigger('click');
            })
            .end().eq(2).click(function(){
                $('input[typw="button"]:eq(0)').unbind('click');
            })
            .end().eq(3).toggle(function(){
                $('.panel').hide('slow');
            },function(){
                $('.panel').show('slow');
            });
        })
    
    </script>
    <li class="panel">welcome to jQuery!</li>
    
    		
  •  

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐