对于新动态创建的元素,Click事件不会持久存在

时间:2013-03-05 20:15:34

标签: javascript jquery

我有一些代码附加到类的click事件:

$(".filter-link").click(function (e) {
    $("#filter").dialog("option", "position",
                    { my: "left top", at: "left bottom", of: e });
    $("#filter").dialog("open");
});

我经常动态重新创建我使用它的元素:

function addTopLinks() {
    $('#calendar .fc-view thead th.fc-resourceName')
        .removeClass('top-calendar-cell');
    $('#calendar .fc-view thead th.fc-resourceName')
        .addClass('top-calendar-cell');
    $('#calendar .fc-view thead th.fc-resourceName')
        .html('<a class="filter-link" href="#">Filter Resources</a>');
};

有什么方法可以让点击持续存在,或者每次重新创建元素时我都会被迫重新分配点击次数?

由于

1 个答案:

答案 0 :(得分:2)

使用.on()

$(document).on("click", ".filter-link", function (e) {
     $("#filter").dialog("option", "position",
            { my: "left top", at: "left bottom", of: e });
     $("#filter").dialog("open");
});