2

これらのイベントを.live()イベントハンドラーに変更するにはどうすればよいですか?

$(document).ready(function()... to .... $(document).live(function()、.keydown、.dataを.liveに変更できると思いましたが、できません。それが機能するようです...助けてください。

$(document).ready(function(){
                $('#number').bind('keyup change focus', function(){
                        var field = this;
                        timer = setTimeout(function(){runSearch(field);}, 444);});                      
                $('#number').keydown(function(){clearTimeout(timer);});                 
                //url selection by specifics                
            $('#phone_book tr').data('bgcolor', '#aaf').hover(function(){
                 var $this = $(this);
                 var newBgc = $(this).data('bgcolor');
            $(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);
                 });  
                //url selection by specifics  
                $("#phone_book tr").click(function(){
                        var id = $(this).children().first().html();
                        var type = $(this).parents('table').siblings('div.module_header').html().toLowerCase();
4

3 に答える 3

6

変化する

.bind(...

.on(...

.live()は実際には非推奨です。

.keyup()、. keydown()のようなものの場合は、それらを.on('keydown'...などに変更します。

于 2012-12-20T19:40:20.927 に答える
1

.on関数を使用したコードの例を次に示します。

$(document).ready(function(){
      $('#number').on('keyup change focus', function(){
             var field = this;
             timer = setTimeout(function(){runSearch(field);}, 444);});                      
            $('#number').keydown(function(){clearTimeout(timer);});                 
                //url selection by specifics

            $('#phone_book tr').data('bgcolor', '#aaf').hover(function(){
                    var newBgc = $(this).data('bgcolor');
                    $(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);});  
                //url selection by specifics  
            $("#phone_book tr").on('click', function(){
                     var id = $(this).children().first().html();
                     var type = $(this).parents('table').siblings('div.module_header').html().toLowerCase();
于 2012-12-20T20:19:04.650 に答える
1

.bind()を.live()に変更します

live()は新しいバージョンのjQueryでは非推奨であり、代わりにon()を使用する必要があることに注意してください。一部のイベントでは、古いデリゲート()メソッドも必要になる場合があります。

$('#number').live('keyup change focus', function(){
    var field = this;
    timer = setTimeout(function(){runSearch(field);}, 444);});                      
    $('body').delegate('#number', 'keydown', function(){clearTimeout(timer);});                 
        //url selection by specifics                
        $('body').data('bgcolor', '#aaf').delegate('#phone_book tr', 'hover, function(){
            var $this = $(this);
            var newBgc = $(this).data('bgcolor');
            $(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);
    });  
于 2012-12-20T19:42:42.420 に答える