Event handling issues (Javascript) -


OK, so I have a couple, which ended up overwhelming questions about conflicting event handling for this forum Can do. / P>

As I understand, a proper set-up document will look something like this:

  & lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Title & lt; / Title & gt; & Lt; / Body & gt; & Lt; Script src = "jsfile.js" type = "text / javascript" gt; & gt; & lt; / head & gt; body & gt; // body content, like something in my case Form element & lt; / html> gt;  

Jsfile.js will look something like this:

  function A () {// code;} function Document.myForm.typeSel.addEventListener ('change'); add.EventListener ('click', b, wrong); get.ElementById ("id"); Window.addEventListener ('load', one, false) , C, false); // or to use better browser-compatible code ... function addEvent (obj, evt, fn) {if (obj.addEventListener) obj.addEventListener (evt, fn, false); If (obj.attachEvent) obj.attachEvent ('on' + Iwat, F Advert (document, 'load', 'A'); Event (document). IEID; ID;, 'click', b); Advent (document.moform.exe, 'change', c); < / Code> 

As I understand it, while the browser in the head will load this JavaScript code, it will add all the event handlers to their respective elements. However ... while Win Dow handler is properly connected, none of the other but if within a function, (for example) an element The getElementById method works fine to access, and the event handler is added. So I can create a Load Aventus () function which is called through window oll, in which all the addEvent () functions for other document elements are included, for which I need the event handler. But as I like the whole thing I understand, I should not have to do this.

In addition to this, if I paste the element code along with the element, as it does, like ...

  & lt; Input type = "checkbox" id = "test" /> & Lt; Script type = "text / javascript" document.getElementById ("test"). Onclick = func;   

... then it works fine It also violates the whole reason for removing inline event handlers!

The question is going to be: " element .addEventListener ('click', func, false) "AddEvent ( element , 'click', func)", or even " element .onclick = func" - to use the script file In the end, how to successfully reference an element Can I other without rod function, why out of the head, Elementbiaiaidi and other ways a function in the head do not work?

Or, there is some flaw in my underlying understanding?

and

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -