2012-06-13 72 views
3

我似乎無法獲取IE8以防止執行onclick函數。嘗試了許多代碼變體。任何幫助,將不勝感激。現在,當我點擊鏈接時,我會看到不應該發生的警告彈出窗口。防止IE8中的onclick事件

<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; 
}); 
+0

alert(event.stopPropagation);'show?爲什麼在代碼中需要兩個事件處理程序? –

+0

[This](http://stackoverflow.com/questions/1756425/prevent-onclick-action-with-jquery/)將幫助和[this](http://stackoverflow.com/questions/387736/how-to -stop-event-propagation-with-inline-onclick-attribute /)too :) – Jashwant

回答

0

據我瞭解,stopPropagation()cancelBubble用於進一步往上走的聽衆鏈停止活動,但仍將執行目前的監聽器。你有沒有試過event.preventDefault()

+0

是的,那也行不通。我認爲需要發生的事情是需要首先刪除事件處理程序,但不知道如何讓IE8工作。例如,以下代碼適用於ff和chrome,但不適用於IE。 (功能(e){ \t this.onclick = undefined; \t});(每個功能(e){ $('a [onClick^= openMoreProductVideos]') – septemberbrain

1

只是嘗試this..removing其他條件..

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

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


}); 

檢查的jsfiddle .. jsfiddle.net/kabichill/qu7kP

+0

希望它是那麼簡單,但那是行不通的。 – septemberbrain

+0

檢查這個小提琴http://jsfiddle.net/kabichill/qu7kP/ ....它的工作.. –

0

這工作正常,我在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 :僅供參考,我被if(e.cancelBubble)卡住了IE,它必須是if(e.cancelbubble!= null)