Jquery.live () interaction events

Let's say I have a scenario where I have a global plugin (or at least a plugin that communicates with a wider array of events).

This plugin accepts a selector and binds a live click to it. Something in pseudo-jquery that might look like this:

$.fn.changeSomething = function(){
     $(this).live("change", function(){ alert("yo");});
}

On another page, I have an extra live anchored something like this:

$("input[type='checkbox']").live("click", function(){alert("ho");});

In this scenario, the check box is ideal for both live events.

What I see is that the change event is triggered as it should, and "years" warn me. However, using this live click, I never launch it. However, using explicit click bindings, I do this.

An easy workaround is to fire the click event at the end of the real-time change handler, but that seems crazy to me. Any ideas?

Note that this is using jquery 1.4.2 and only happens in IE8 (I assumed 6/7 as well, but I haven't tested them).

example (you will need jquery-1.4.2.min.js):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $.fn.changeSomething = function(){
        var t = $(this);
        t.live("change", function(){
            alert("yo");
        }); 
    };

    $(document).ready(function(){
    $("input[type='checkbox']").changeSomething();
    $("#special").live("click", function(){
      alert("ho");
    });
    });
</script>
</head>
<body>
<form>
  <input type="checkbox" id="cbx" />
  <input type="checkbox" id="special" />
  </form>
</body>
</html>
+5
source share
3 answers

You know that IE will not fire the "change" event until the flag loses focus, right?

change , while I think it's true, the effect of this test page is rather strange. I am still playing with it. The living mechanism confuses me and makes me a little nervous, although I fully understand its value.

( ) : http://gutfullofbeer.net/clicks.html, jQuery

: , "change" body:

$('body').bind('change', function() { return true; });

. , @Alex , - , jQuery "". . http://gutfullofbeer.net/clicks-body.html, , " ", "click" , " " , .

+5

$.delegate $.live

live , , . document, , , , .

, IE - , . IE "" , jquery, . -, . , , IE. , ...

+1

jquery.livequery

$("select[name='majorsList']").livequery("change",function()
{
alert('in');
});
0

All Articles