代码之家  ›  专栏  ›  技术社区  ›  JD Isaacks

JQuery不适用于页面加载后创建的元素

  •  1
  • JD Isaacks  · 技术社区  · 15 年前

    $(".month-selector").change(function(){
        setStones();
    });
    

    在里面 $(document).ready()

    $("#month-selectors").html(month_selectors);
    
    $(".month-selector").change(function(){
        setStones();
    });
    

    还是不行。但是,如果我创建一个静态的,它就会工作。

    3 回复  |  直到 15 年前
        1
  •  14
  •   Brian Flanagan    15 年前

    您可以使用jQuery的'live()'方法向当前和将来的节点添加事件侦听器。

    $(".month-selector").live('change', function(){
        setStones();
    });
    
        2
  •  2
  •   Siddharth Bhansali    15 年前

      $("input#thename").live("click", function(event) {
    // handler for what should happen when somebody clicks on the new input field, in this case I needed a datepicker to pop up so lets get that fired. Now that the click element registered the new input object with the DOM, it can be used like a normal jQuery selector!
        $("input#thename").datepicker();
        });
    
        3
  •  0
  •   zerodin    15 年前

    尝试:

    $(".month-selector").live("change", function(event) {
     // some code
    });