動的に生成します
<input type="text" id="foo">
<label for="foo">
そして、リスナーを入力に設定します:
updateWithLabelInputs($("#foo"));
そして機能:
function updateWithLabelInputs(withLabel) {
withLabel.die("focus").live("focus", function (e) {
$(this).parent().find('label').hide();
});
withLabel.die("blur").live("blur", function (e) {
if ($(this).val() == '') {
$(this).parent().find('label').show();
}
});
}
ラベルをクリックすると、イベントが発生するはずですが、発生しません。興味深い: 同じリスナーが既存の (動的ではない) html に適用される場合、動作します。何が問題ですか?リスナーが動的要素に対して機能しないのはなぜですか?