简体   繁体   中英

Prevent onclick event in IE8

I can not seem to get IE8 to prevent the onclick function from executing. Tried numerous code variations. Any help would be appreciated. As it stands now when I click on the link I get the alert popup which should not be happening.

 <p><a href="#" onclick="openMoreProductVideos('Video title','videocode','false');return false;"><span class="txt">View all videos</span></a></p>

function openMoreProductVideos(title,productCode,fastprevoew) 
{  
  alert ("openMoreProductVideos - should not see this!");
}

$('a[onclick^=openMoreProductVideos]').click(function (event) {
   if (event.stopPropagation){
         event.stopPropagation();
   }
   else if(window.event){
      window.event.cancelBubble=true;       
   }       
    return false;
});

just try this..removing the other conditions..

$('a[onclick^=openMoreProductVideos]').click(function (event) {

   event.preventDefault();      //will prevent the event from occuring


});

check the jsfiddle .. jsfiddle.net/kabichill/qu7kP

As I understand it, stopPropagation() and cancelBubble are used to stop the event from going further up the chain of listeners, but will still execute the current listener. Have you tried event.preventDefault() ?

This works fine for me in IE8+ & FF

function stopEvent(e) {    
    if (!e) e = window.event;

    if (e.cancelBubble != null) e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation(); //e.stopPropagation works in Firefox.
    if (e.preventDefault) e.preventDefault();
    if (e.returnValue != null) e.returnValue = false; // http://blog.patricktresp.de/2012/02/
    return false;
}

IMP : FYI, I got stuck with if(e.cancelBubble) for IE, it has to be if(e.cancelbubble != null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM