javascript - Prototype vs jQuery: A specific problem regarding binding, events and values -
i'm looking @ transferring existing functionality (built prototypejs) jquery , have been working through couple of examples.
one example i'm bit stuck on, regards binding functions (and values) event. example tries apply mouseover event, incremented value list of links. value alerted want same every time (so link 1 alerts "1", link 2 alert "2" etc etc)
here's snippet of simple example.
prototype:
findlinks: function () { = 1; $$("#vertical-content-links ul li a").each(function (s) { event.observe(s, "mouseover", this.activatelink.bindaseventlistener(this, i)) i++ }.bind(this)) }, activatelink: function (e, i) { alert(i) }
within example, when link 1 mouseovered, alert "1". link 2 alert "2" etc etc alerting value of when function applied event.
however, similar code in jquery....
findlinks: function () { = 1; $("#vertical-content-links ul li a").each(function (s, elmt) { $(elmt).bind("mouseover", function (e) { _this.activatelink(i); i++ }) }) }, activatelink: function (i) { alert(i); }
(note: _this closure specified elsewhere in code)
when runs, every time link mouseovered, value of incremented one, link 1 alert 1, 2,3,4,5 etc value of doesn't seem bound function when applied event
hope makes sense
does know way around works more way prototype does. want start using jquery more need understand issue first.
cheers
i bet gets incremented because put i++
inside mouseover handler. carry out , try this:
findlinks: function() { var = 1; $("#vertical-content-links ul li a").each(function(s, elmt) { var j = i++; $(elmt).bind("mouseover",function(e) { _this.activatelink(j); }) }) },
Comments
Post a Comment