如何在动态包含div之后加载js .click

时间:2013-05-31 14:00:43

标签: javascript android jquery html jquery-mobile

我有一个div,用jquery .load事件将内容加载到其中。

示例:

<div data-role="content">
<!-- dinamic loaded content with .load-->
    <input type="button" id="hi" />
<!-- end of loaded content-->
</div>

我在标题上有一个.js加载的承诺

$(function() { 
   $("#hi").click(function() {
       alert("hi");
  });
});  

由于在加载js时div不存在,因此点击事件不会触发,解决方案是在jquerymobile点击事件上?我该怎么做才能解决这个问题?

由于

1 个答案:

答案 0 :(得分:2)

使用jQuery .on()

$(document).on('click', '#hi', function(){
     alert('hi');
})

我选择document但它应该是最接近的非动态父级。