如何让jQuery在尚未创建的对象上工作?

Ang*_*ela 0 javascript jquery

可能重复:
在jQuery中绑定动态创建的元素

有没有办法可以更改以下代码:

$('#city')
        .focus(function () {
            $('option[value="99"]', this).remove();
            store.setItem($(this).attr('id'), $(this).val());
        })
        .change(function () {
            store.setItem($(this).attr('id'), $(this).val());
            $(this).attr("title", $("option:selected", this).attr("title"));
            $('#detailData').html("");
        });
Run Code Online (Sandbox Code Playgroud)

因此,只要它们具有"update-title"类,即使它们尚未创建,它也适用于选择.例如:

<select class="update-title"> 
Run Code Online (Sandbox Code Playgroud)

我看到了一些使用live的实现,但是有人说使用它并不好.这样做也有很多开销.在我确定使用document.ready()创建选择后,添加代码会不会更好?

Dan*_*ite 6

看看on.这是替代品live,可以为您做到这一点.