2013-11-25 19 views
1

我有一個jsp頁面,下面是html和js。現在我想把js函數test()保存到另一個js文件中。我試圖用jquery寫這個。javascript:無效(0)在jquery

<div class="class1"> 
<a href="javascript:void(0)" onclick="test();" id="add_another" ><b>Add another user</b></a> 
</div> 

JS功能

function test(){ 
    alert("I am here"); 
    //other code 
} 

我試着寫這樣

$(function() { 
    $('#add_another').click(test); 
}); 

但它不工作。我不確定它是否因爲其具有href =「javascript:void(0)」 當用戶單擊添加其他用戶鏈接時,它會顯示另一個div。這是測試()做的。但警報本身並未到來。 有人可以幫我嗎?

+0

您嘗試過'javascript:;'而不是'javascript:void(0)'嗎? – fnkr

+1

它爲我工作。請檢查http://jsfiddle.net/Ub6Fk/ –

回答

4

要複製javascript:void(0),您可以在這樣的js中使用event.preventDefault()

$(function() { 
    $('#add_another').click(function (e) { 
     e.preventDefault(); 
     test(); 
    }); 
}); 
+0

爲什麼倒票? –

+1

這是實現這一目標的最佳途徑。 – Sean

相關問題