Sunday 15 August 2010

Can I flush the event stack within Firefox using Javascript? -


There is a hierarchy of tags within my HTML that contains all the onclick event handlers. On the page through the root of the hierarchy On the rotation back, the event is pushed onto the stick on the stick. I just want to answer the event on the leaf click event. Can I flush the event stack instead of using the flag?

For example ...

  & lt; Ul & gt; & Lt; Li onclick = "Nada ('1');" & Gt; & Lt; A href = "# 1" & gt; 1 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li onclick = "nada ('2');" & Gt; & Lt; A href = "# 2" & gt; 2 & lt; / A & gt; & Lt; Ul & gt; & Lt; Li onclick = "Nada ('2.1');" & Gt; & Lt; A href = "# 2.1" & gt; 2.1 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li onclick = "Nada ('2.2');" & Gt; & Lt; A href = "# 2.2" & gt; 2.2 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li onclick = "Nada ('2.3');" & Gt; & Lt; A href = "# 2.3" & gt; 2.3 & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; Li onclick = "Nada ('4');" & Gt; & Lt; A href = "# 4" & gt; 4 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li onclick = "Nada ('5');" & Gt; & Lt; A href = "# 5" & gt; 5 & ​​lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt;  

Clicking 2.2 using this function ...

  function nada (which) {warning (which); }  

... will result in two alerts for '2.2' and '2'.

Do I '2' in Nada Festival?

To prevent the incident from bubbling parent elements, you to tell the incident < / Code> About the object in IE, you set event.cancelBubble = true . In other browsers, you call event.stopPropagation () .

You might want to disable the default link-the following action so that the browser can not keep jumping on top of following non-existing anchor links such as # 1 . In IE, you set event.returnValue = false in other browsers, you call event.preventDefault () .

Event object is accessible as window.event i.e. On other browsers, it is passed in the event handler function. One way to pass an event into a function that works on both:

  & lt; Li onclick = "Nada ('2.1', incident);" & Gt; & Lt; A href = "# 2.1" & gt; 2.1 & lt; / A & gt; & Lt; / Li & gt; Function nada (n, event) {alert (n); If ('StopPropagation' in the event) {event.stopPropagation (); Event.preventDefault (); } And {event.cancelBubble = true; Event.returnValue = false; }}  

However, it would be better all round to put an onclick event on the a element, which is usually related to it . It helps to access because the a element will be focusable and keyboard-operated and this means that you do not have to worry about parents, click the handler called is.

(You can style a to look like a plain block if you want.)

You need unnecessary with unneeded scripting Onclick can also be linked to:

  & lt; Ul id = "nadalist" & gt; ; & Lt; Li & gt; & Lt; A href = "# 1" & gt; 1 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "# 2" & gt; 2 & lt; / A & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "# 2.1" & gt; 2.1 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "# 2.2" & gt; 2.2 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "# 2.3" & gt; 2.3 & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "# 4" & gt; 4 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "# 5" & gt; 5 & ​​lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; Script type = "text / javascript" & gt; Var link = document.getElementById ('Nadlist'). GetElementsByTagName ('A'); (Var i = links.length; i ->;;) {link [i] .onclick = function (warnings (this.hash.substring (1)); return false; }} & Lt; / Script & gt;  

No comments:

Post a Comment