The other day I've started a discussion on the jQuery dev list about event delegates and how the live() function can be improved with a few little tweaks from Zach Leatherman Blog - Performance Caveat with jQuery Selectors and Live Events

After reading at Zach's Blog I've was able to create a little plugin that I think will also be useful to those who are looking for an alternative way to bind live events to an element without first querying the document.

Example:


    $('#sidebar').delegate('li :click',callback); 

From my test in FF 3.5 and IE7 I was able to bind an event to a ul element with over 800 li elements with excellent performance!

Here's how to use the plugin:


    $(context).delegate('click',callback)  // same as live()

    $(context).delegate('li:even, li:odd :click',callback)  // supports multiple selectors

    $(context).delegate('li :click.namespace',callback)  // supports event namespace

    $(context).delegate('li:first a :click',callback)  // supports pseudo classes 

    $(context).delegate('li a :click mouseover',callback) // support future multiple event format 

The delegate method will use the same rules as that of the live() method.

Note: jQuery live() only supports single events: Unlike .bind(), only a single event can be bound in each call to the .live() method.

Here's the plugin code:


    <script type="text/javascript">
        jQuery.fn.delegate = function(event,fn) {
            var e,p,s;
            if (event.indexOf(':')) {
                s = event.substr(0,event.lastIndexOf(':'));
                event = event.replace(s+':','');
            }
            if (!s) e = this;
            else {
                p = this.selector + ' ';
                if (!s.indexOf(',')) s = p + s;
                else s = p+(s.split(/,/)).join(','+p);
                e = jQuery(document); e.selector = s
            }
            e.live(event,fn);
            return this;
        }
    </script>        

Working example:


    <script type="text/javascript">
        $(function(){
            $('#sidebar').delegate('li :mouseover',cb);
            $('#sidebar').delegate('li :mouseout',cb);
            //$('#sidebar').delegate('li:first, li:last :click',cb);
        });

        function cb (e){
            var c = (e.type=='mouseout') ? '#fff':'#ffcc00';
            $(e.target).css('background',c);
        }  
    </script>        

    <div id="sidebar">
        <ul>
            <li>item 1</li>
            <li>item 2</li>
            <li>item 3</li>
            <li>item 4</li>
            <!-- copy and paste more li elements here -->
        </ul>
    </div>        
nike air jordan 5 retro online kopen
Posts: 20
Comment
Website385
Reply #8 on : Thu December 20, 2018, 01:02:04
<a href="http://www.e-kocaeli.com/online/nike-negras-2016">nike negras 2016</a><a href="http://www.e-kocaeli.com/op-te-slaan/nike-air-huarache-3-homme-blanc-chaussures">nike air huarache 3 homme blanc chaussures</a><a href="http://www.e-kocaeli.com/pas_cher/teris-nike-air-max-plus-tn-hombre-cargando-zoom">teris nike air max plus tn hombre cargando zoom</a><a href="http://www.e-kocaeli.com/p?-nett/air-max-bw-classic-noir-grant-fr-1">air max bw classic noir grant fr 1</a>
[url=http://www.jessicacade.com/halpa/nike-air-jordan-5-retro-online-kopen]nike air jordan 5 retro online kopen[/url]
navy pink air revolution sky wmns wedge 599410 400 nike
Posts: 20
Comment
Zebra728
Reply #7 on : Wed December 26, 2018, 20:15:19
<a href="http://www.presidentialcaregivers.com/mannen/bleu-gris-blanc-enfant-nike-air-max-90-gamma-blue-junior-baskets-discount">bleu gris blanc enfant nike air max 90 gamma blue junior baskets discount</a><a href="http://www.presidentialcaregivers.com/miehet/nike-air-jordan-6-retro-high-heels-blackwhite-kopen-nike-jordan-high-heel">nike air jordan 6 retro high heels blackwhite kopen nike jordan high heel</a><a href="http://www.presidentialcaregivers.com/mujer/nike-free-nano-zalando">nike free nano zalando</a><a href="http://www.presidentialcaregivers.com/m?n/adidas-neo-caflaire-sneaker-herren-wei%c3%9f-dunkelgrau">adidas neo caflaire sneaker herren wei? dunkelgrau</a>
[url=http://www.nextstage-edu.com/trampki/navy-pink-air-revolution-sky-wmns-wedge-599410-400-nike]navy pink air revolution sky wmns wedge 599410 400 nike[/url]
adidas 3st 002 pk
Posts: 20
Comment
Unity423
Reply #6 on : Fri January 04, 2019, 20:33:27
<a href="http://www.tomenthomas.com/a-buon-mercato/billigt-sko-herr-sneakeradidas-seeley-skor-nej-8537">billigt sko herr sneakeradidas seeley skor nej 8537</a><a href="http://www.tomenthomas.com/barato/nike-air-force-1-low-nike-air-force-1-lo-blackblack-fs3gm">nike air force 1 low nike air force 1 lo blackblack fs3gm</a><a href="http://www.tomenthomas.com/billig/moda-nike-janoski-hombre-venta-banana623">moda nike janoski hombre venta banana623</a><a href="http://www.tomenthomas.com/boutique/nike-huarache-run-damen-g%c3%bcnstig">nike huarache run damen g?nstig</a>
[url=http://www.namdevnews.com/gymnastikskor/adidas-3st-002-pk]adidas 3st 002 pk[/url]
material?berlegenheit adidas stan smith trainers off white p
Posts: 20
Comment
Enjoy319
Reply #5 on : Fri February 08, 2019, 23:24:19
<a href="http://www.pro-healthchiropractic.com/uppkopplad/ah8050-401-nike-air-max-270-blau-herren-freizeitschuhe">ah8050 401 nike air max 270 blau herren freizeitschuhe</a><a href="http://www.pro-healthchiropractic.com/uusi/calidad-barato-hombres-nike-hyperdunk-2016-team-golddark-greywhite">calidad barato hombres nike hyperdunk 2016 team golddark greywhite</a><a href="http://www.pro-healthchiropractic.com/vrouw/nike-air-max-2017-dark-blue-gray-mens-best-nike-shoes-576">nike air max 2017 dark blue gray mens best nike shoes 576</a><a href="http://www.pro-healthchiropractic.com/zni?ka/nike-huarache-come-nuove-numero-40-ma-vestono-molto-meno-a">nike huarache come nuove numero 40 ma vestono molto meno a</a>
[url=http://www.agenberasmurah.com/uusi/material%c3%bcberlegenheit-adidas-stan-smith-trainers-off-white-p]material?berlegenheit adidas stan smith trainers off white p[/url]
hombre nike roshe one zapatos casuales caqui avena blanco 511881 203
Posts: 20
Comment
Better869
Reply #4 on : Sat March 23, 2019, 06:30:59
<a href="http://www.sonossupportdesk.com/particular/nike-air-jordan-4-retro-levis-white-levis-tag-dame">nike air jordan 4 retro levis white levis tag dame</a><a href="http://www.sonossupportdesk.com/photo/nike-air-max-90-hoch-tops-schuhe-m%c3%a4nner-alle-wei%c3%9f-schuhe">nike air max 90 hoch tops schuhe m?nner alle wei? schuhe</a><a href="http://www.sonossupportdesk.com/puma/nike-jr-hypervenom-phantom-iii-academy-fg-uomo">nike jr hypervenom phantom iii academy fg uomo</a><a href="http://www.sonossupportdesk.com/roshe/puma-glyde-hi-polka-dot-m%c4%99skie">puma glyde hi polka dot m?skie</a>
[url=http://www.engineeredtoinnovate.com/adidas/hombre-nike-roshe-one-zapatos-casuales-caqui-avena-blanco-511881-203]hombre nike roshe one zapatos casuales caqui avena blanco 511881 203[/url]
nike womens grey white pink air max tn
Posts: 20
Comment
Desc814
Reply #3 on : Fri March 29, 2019, 08:55:18
<a href="http://www.hayatguzelse.com/en-us-shop/air-max-180-black-orange">air max 180 black orange</a><a href="http://www.hayatguzelse.com/factory-sale/find-nike-air-max-90-vt-mens-sneakers-army-camo">find nike air max 90 vt mens sneakers army camo</a><a href="http://www.hayatguzelse.com/fake/exciting-air-max-97-nike-black-gray-moon-branch-shoes">exciting air max 97 nike black gray moon branch shoes</a><a href="http://www.hayatguzelse.com/fashion-deals/buy-womens-nike-air-max-motion-shoes-deep-grey-green-outlet">buy womens nike air max motion shoes deep grey green outlet</a>
[url=http://www.ibizayachtweek.com/6pm-shoes/nike-womens-grey-white-pink-air-max-tn]nike womens grey white pink air max tn[/url]
nike zoom lebron soldier 10 id
Posts: 20
Comment
Integration383
Reply #2 on : Tue April 02, 2019, 10:18:34
<a href="http://www.awesomehealthylife.com/discount-website/nike-free-3.0-v3-mens-black-red-shoes">nike free 3.0 v3 mens black red shoes</a><a href="http://www.awesomehealthylife.com/en-category/nike-kwazi-basketball-shoes-mens-night-maroon-black-vr">nike kwazi basketball shoes mens night maroon black vr</a><a href="http://www.awesomehealthylife.com/features/real-nike-air-jordan-1-high-black-cement-white-varsity-red-839115-013">real nike air jordan 1 high black cement white varsity red 839115 013</a><a href="http://www.awesomehealthylife.com/footlocker/mens-nike-air-vapormax-plus-cool-grey-shoes">mens nike air vapormax plus cool grey shoes</a>
[url=http://www.andrewclotempio.com/footlocker/nike-zoom-lebron-soldier-10-id]nike zoom lebron soldier 10 id[/url]
mens nike air max 95 ultra essential blue shoes
Posts: 20
Comment
Join()394
Reply #1 on : Wed April 10, 2019, 06:18:51
<a href="http://www.pierangeliangsen.com/butik/nike-zoom-air-generation-pas-cher">nike zoom air generation pas cher</a><a href="http://www.pierangeliangsen.com/butikk/nike-roshe-run-arancione">nike roshe run arancione</a><a href="http://www.pierangeliangsen.com/chaussure/nike-air-force-xxv-baratas">nike air force xxv baratas</a><a href="http://www.pierangeliangsen.com/en-ligne/nike-air-pegasus-89-wit">nike air pegasus 89 wit</a>
[url=http://www.kamagracuanto.com/replica/mens-nike-air-max-95-ultra-essential-blue-shoes]mens nike air max 95 ultra essential blue shoes[/url]
First | Previous | Showing comments 11 to 18 of 18

Leave a Comment

Required fields are marked with *.
If you have trouble reading the code, click on the code itself to generate a new random code.