2012-09-10 70 views
0

我制作jQuery代码时添加HTML elemnet button2当事件点击按钮1时, 并且另一个向DIV添加另一个HTML元素时点击按钮2 第一个运行但第二个不运行。如何解决这个问题呢 ?在追加按钮后调用函数

的问题实例

var button2='<input type="button" vale="add" id="idbutton2" >'; 
$('#idbutton1').click(function(){ 
    $('#divid1').html(button2); 
}); 

$('#idbutton2').click(function(){ 
    $('#divid2').html("text"); 
}); 

的BUTTON2增加DIV,我想,当点击按钮2时运行jQuery的。

+0

可能重复[jquery没有点击行为](http://stackoverflow.com/questions/9083768/jquery-no-behavior-on-click) – undefined

回答

0

将其更改为:

$('#divid1').on('click', '#idbutton2', function(){ 
    $('#divid2').html("text"); 
}); 
0

使用.on而不是委派的事件处理程序。

$(document).on('click', '#idbutton2', function(){ 
    $('#divid2').html("text"); 
}); 

或者你可以直接绑定到button2。

var button2= $('<input type="button" vale="add" id="idbutton2" >').click(function(){ 
    $('#divid2').html("text"); 
}); 

$('#idbutton1').click(function(){ 
    $('#divid1').html(button2.html()); 
});