Jquery.each () delegate syntax

I'm having trouble getting write syntax for the equivalent of $ .each using delegates.

All the examples I read just show a click or a hang, but how would I start writing this using delegate syntax?

$("ul li a").each(function(){

    $(this).addClass("foo");

});

Hooray!!

Edit: after a lot of comments (thanks, by the way), I thought it was better to post a more explicit example. We apologize for the lack of detail in my previous question.

Say, for example, I want to snap tooltips to anchors on a page. I will have many elements that I want to configure as hints, and I want to reduce memory overhead by using delegation.

As I understand it, so I would start:

$(document).delegate("a.tooltip", "click", function(event){

// run click event

}).delegate("a.tooltip", "hover", function(event){

// run mousenter, mouseleave events.

});

, title, . , ( ), hover, , .

- :

$("a.tooltip").each(function () {
    // Store our title attribute and remove it so we don't get browser 
    //tooltips showing up.
    $.prop(this, "data-old-title", $.attr(this, "title"));
}).removeAttr("title");

?

, , , - .

+3
3

, , , .delegate() . , DOM, DOM, .

, <div>, <button> , .delegate() click <div>.

, , , , , DOM, , event.stopPropagation(). - <div>, . , , , ( ), , , , .

, , DOM . DOM, , , .

+6
$("ul").delegate("a", "hover", function(){
    $(this).addClass("hover");
});
0

No jQuery for .delegate()which would be equivalent.each()

The delegate allows you to attach event handlers to objects that exist now or in the future.

Each lets you scroll through a collection of objects

If you want to add a class footo the anchor tag based on some event, then you can do something like:

$('ul li').delegate('a', 'click', function(){
    $(this).addClass("foo");
});
0
source

All Articles