请给我一点想法。 在这种情况下我该怎么做。
var buttons = document.getElementsByName("buttons"); //get four buttons
buttons[0].onclick = alert("hoge");
buttons[2].onclick = alert("hoge");
buttons[1].onclick = alert("fuga");
buttons[3].onclick = alert("fuga");
答案 0 :(得分:1)
就像其他任何任务一样:
a = b = c = d = e = f = "letters!";
适用于您的情况:
buttons[0].onclick = buttons[2].onclick = function() {alert("hoge");};
答案 1 :(得分:0)
你可以做到
buttons[0].onclick = buttons[2].onclick = alert("hoge");
buttons[1].onclick = buttons[3].onclick = alert("fuga");
答案 2 :(得分:0)
另一个选择
var buttons = document.getElementsByName("buttons"); //get four buttons
//Add buttons to own array depending on which alert to display
var buttonsHoge = [buttons[0],buttons[2]];
var buttonsFuga = [buttons[1],buttons[3]];
//Loop through each array and attach the alert to the buttons click event
jQuery.each(buttonsHoge,function(){
$(this).click(function(){ alert("hoge"); });
});
jQuery.each(buttonsFuga,function(){
$(this).click(function(){ alert("fuga"); });
});