【问题标题】:How to load js .click just after a div is dynamically included如何在动态包含div之后加载js .click
【发布时间】:2013-05-31 14:00:42
【问题描述】:

我有一个 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点击事件上?我该怎么做才能解决这个问题?

谢谢

【问题讨论】:

    标签: javascript android jquery html jquery-mobile


    【解决方案1】:

    使用 jQuery .on()

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

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

    【讨论】:

    • 谢谢,工作完美,它也没有被包含的css设置样式,有什么解决方法吗?
    • 什么CSS?如果您使用 CSS 文件设置动态 div 的样式,则应该没有任何问题。
    • @Enoque Duarte:作为另一个问题提出这个问题,有人会告诉你你的造型有什么问题。
    • 谢谢我的朋友们=),我明白了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多