5

ユーザーが入力のクリックに集中するたびに、メソッドをトリガーしたいと考えています。次のコードがあります。

jQuery(document).on('focus click', function(e){
        console.log("focused on " + $(e.target).attr('id'));
});

しかし、私が要素に焦点を当てるときはいつでもfocused on undefined. このコードの何が問題になっていますか?

4

2 に答える 2

7

on()を使用して bind イベントで入力セレクターを見逃しました。

jQuery(document).on('focus click', 'input',  function(e){
        console.log("focused on " + $(e.target).attr('id'));
});

on() の構文.on( events [, selector ] [, data ], handler(eventObject) ), reference

于 2013-02-18T12:03:02.367 に答える
6

試す:

$(document).on('focus click', 'input',  function(e){
        console.log("focused on " + e.target.id);
});

また、それだけe.target.idで十分です..

于 2013-02-18T12:03:40.033 に答える