English 中文(简体)
this, current context-when should I use in jQuery?
原标题:

I am not very sure with the use of "this" [current context] in jquery.What I know is- it prevents the dom from searching all the elements, it just work on that current element, which improve performance[correct me if I am wrong].Also I am not sure when to use this and when not. lets say, should I go for

  $("span",this).slice(5).css("display", "none")

or

 $("span").slice(5).css("display", "none")

both will work, but I am not very clear as how really it works.can somebody explain it with a diff/proper example, and when to use what? [EDIT]

      $(function() {
        $("#clickme").click(function() {
            $("span",this).slice(5).css( display ,  block );//doesn t work ? why?
             $("span").slice(5).css( display ,  block );//works..why?

        });
    });
enter code here <span id="clickme">Click me</span>
                 <span>itam1</sapn>
                <span>itam2</sapn>
                <span>itam3</sapn>
                 <span>itam4</sapn>
                    <span>itam5</sapn>
                    ...upto10
最佳回答

Usually you can use the this keyword on event handlers since it will be a reference to the element that triggered the event and other jQuery functions like $.each.

For example when handling a click event lets say:

$( .parentElement ).click(function () {
  $( .foo , this).hide();
});

The above code, will hide all the elements with class foo that are descendants of the currently parentElement that was clicked.

The use of the context argument of the jQuery function is the equivalent of making a call to the find method:

$(expr, context);
// is just equivalent to:
$(content).find(expr);

EDIT: Looking at your example:

    $("#clickme").click(function() {
      $("span",this);//... (1)
      $("span");//..       (2)
    });

The first line, will look for all the span elements that are inside of #clickme (its descendants), since that element was the one that triggered the click event.

The second line, will look for all the span elements on the whole page.

问题回答

How it works

Lets use this HTML for the examples:

<div id="container">
   <div class="column"><a href="#">Link 1</a></div>
   <div class="column"><a href="#">Link 2</a></div>
</div>
<div id="footer">
   <a href="#">Link 3</a><a href="#">Link 3</a>
</div>

The scoping parameter of the jQuery function should only be used if you already have a cached reference to a DOM element or jQuery wrapped element set:

var $set = $( #container );
$( a , $set).hide(); // Hides all  a  tag descendants of #container

Or in an event:

$("#container").click(function(e){
   $( a , this).hide(); // Same as call above
}

But it makes no sense to use it like this:

$( a ,  #container ).hide()

When it should be written like this:

$( #container a ).hide();

Having said all that, it is generally cleaner and clearer to just use .find() instead of using the second parameter in the jQuery function if you already have the jQuery or DOM element. The first example I gave would be written this way instead:

var $set = $( #container );
$set.find( a ).hide(); // Hides all  a  tag descendants of #container

If this one call was the only reason you grabbed the #container object, you could also write it this way since it will still scope the search to the #container element:

$("#container a").hide(); // This is the same as $( a , "#container");

Why would you scope your selections

When jQuery looks for an unscoped selector, it will search through the entire document. Depending on the complexity of the selector, this could require a lot of searching. If you know that the element you are looking for only occurs within a specific parent, it will really speed up your code to scope the selection to that parent.

Regardless of what method of scoping you choose, you should always scope your selectors whenever possible.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签