What do you think is the best way to link events?
4 answers
If jquery I usually bind like this:
var el = $("#dontCare"); el.on('eventName', function(e){}); This is how I do it for a very simple reason - if custom events are used in the code (and I always use them), this helps to avoid porridge in the code and bring everything to a single form. Those.
el.on('mySuperPuperEvent', function(e){}) // Π½Π° Π³Π»Π°Π· Π½Π΅ ΠΎΡΠΎΠ±ΠΎ ΠΎΡΠ»ΠΈΡΠ°Π΅ΡΡΡ ΠΎΡ el.on('click', function(e){}); // Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ° ΠΊΠ»ΠΈΠΊΠ° // ΡΠΎΠ³Π΄Π° ΠΊΠ°ΠΊ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ el.mousemove(function(e){}) // ΠΈ el.on("mySuperPuperEvent", function(e){}); // ΠΊΠ°ΠΊ Π²ΠΈΠ΄ΠΈΡΠ΅ ΠΏΠΎΠ»ΡΡΠ°Π΅ΡΡΡ ΠΊΠ°ΡΠΊΠΎ... Well, I also use this one because Iβm used to writing nodeJS handlers exactly the same way
In Prototype, it's about the same:
el.observe("eventName", function(e){}); If we are talking about native JS, I do not write on it for old browsers, respectively, I use the w3c standard binding
el.addEventListener("event",function(){e}, false); And the best one that is better read. In the sense that it is not so important how exactly you will do it, it is important that all this be implemented in the same style.
With a small JS code, this is not important at all, but if the code is large, you just need to choose a single style and stick to it everywhere.
Through jikveri, naturally.
usual:
$('selector').event(function(){...}); or
$('selector').bind('event',function(){...});//ΠΠ»Ρ Π±ΠΎΠ»Π΅Π΅ Π»Π΅Π³ΠΊΠΎΠ³ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΡ Π² Π΄ΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈΠΉ. dynamic:
$('selector').live('event',function(){...}); Crossbrowser.
function ae(e,t,f){if(e.addEventListener){e.addEventListener(t,f,false);}else if(e.attachEvent){e.attachEvent('on'+t,function(){f.apply(e)});}else{e['on'+t]=f;}return e;} ae(document.getElementById('id'), 'click', function(){alert(this.innerHTML);}); - function ae (e, t, f) // Wassup? ... Why is SO reduced? - knes
- Expert will now come and fix it on: function ae (e, t, f) // What is going on here? Why reduce the name of the function to two-letter, if it is the task of the program to reduce and optimize the load? - knes
- The letters were drawn in and there were no semicolons .. I see 5 points with commas that can be removed! Oh yeah - even gaps !!! - Zowie
- I just once took the event binding function with javascript.ru and shortened it so that I stuck it out in one line in the program and didnβt light up. =) - ling
- Yes, I understand, just looks "a little" is not readable =) - Zowie
Crossbrowser)
function $(selector) { return document.getElementById(selector); } function Selector() { $('mydiv').onclick = function() { alert(this.innerHTML); } } document.onreadystatechange = Selector;