我附加onclicks按鈕與我想在事件發生時被調用的函數。在運行頁面時,它會繼續執行onclick的功能,而無需單擊。Javascript onclick不點擊執行
window.onload=function(){
var recipeCount = 0;
//gets the recipes and puts them in the menu bar #left
//places them in buttons
$.getJSON('recipes.json', function(data) {
$.each(data.recipe, function(i, f) {
var yep = "</br> <button type='button' id = '" + f.number + "'>" + f.Name + "</button> </br>";
$(yep).appendTo("#left");
recipeCount = recipeCount + 1;
});//for each recipe
});//getJSON
//ends recipes to menu
for(var i = 1; i < recipeCount + 1; i++){
$(i).onclick = clicked(i);
}
$("one").onclick = clicked("one");
};//on load
function clicked(idNum){
$.getJSON('recipes.json', function(data) {
$.each(data.recipe, function(i, f) {
if(idNum == f.number){
var rec = "</br> <h1> " + f.Name + "</h1> " ;
$(rec).appendTo("#bigBox");
}
});//for each recipe
});//getJSON
}//clicked