2012-05-02 60 views
0

單擊事件展望趕上點擊eventfrom這種定位的嵌入一個div內....錨實際上是調用一個模式對話框的形式,我需要剿該調用,這樣彈出不上來。如何捕捉從<div id="whatever"><a class="action-add" href="/x/y/z">add</a></div>

<div id="whatever"><a class=" modal-dialog button action-add" href="/x/y/z">add</a></div> 

<script type="text/javascript"> 
    $("#whatever").click(function (e) { 
     alert('hello'); 
     e.preventDefault(); 
    }); 
</script> 

上面的代碼被調用,但它彈出一個模式窗體/彈出,我想以某種方式剿對錨點擊彈出。我認爲e.preventDefault會這樣做,但彈出窗口仍然出現。

+1

什麼模式彈出? –

回答

2

的問題是,它從錨點擊後傳播您的處理程序時纔會激活,這意味着爲錨的默認行爲仍在發生。

如果你只有DIV中的錨,這將這樣的伎倆:

<script type="text/javascript"> 
    $("#whatever a").click(function (e) { 
     alert('hello'); 
     e.preventDefault(); 
    }); 
</script> 

如果你有別的東西:

<script type="text/javascript"> 
    $("#whatever a").click(function (e) { 
     e.preventDefault(); 
    }); 
    $("#whatever").click(function (e) { 
     alert('hello'); 
    }); 
</script> 
相關問題