简体   繁体   中英

focus / blur don't fire

I'm working on a project that uses the old jQuery 1.3.2 . I need to blur input value on focus and return it (if not changed) on focusout (can't use placeholder here).

This is what I'm doing:

console.log('start');
$('input[type=text]').live('focus', function() {
    console.log('focused');
    if (!$(this).data('defaultText')) $(this).data('defaultText', $(this).val());
    if ($(this).val()==$(this).data('defaultText')) $(this).val('');
});
$('input[type=text]').live('blur', function() {
    console.log('blurred');
    if ($(this).val()=='') $(this).val($(this).data('defaultText')); 
});

I see start in the console, but then nothing on focusing the inputs. What should I change here?

You can use jquery bind in jquery 1.3.2

$('input[type="text"]').bind('focus',function() {
  console.log('focused'); 
 });
 $('input[type=text]').bind('blur', function() {
   console.log('blurd');
 });

Demo

(function(){

    var special = jQuery.event.special,
        uid1 = 'D' + (+new Date()),
        uid2 = 'D' + (+new Date() + 1);

    jQuery.event.special.focus = {
        setup: function() {
            var _self = this,
                handler = function(e) {
                    e = jQuery.event.fix(e);
                    e.type = 'focus';
                    if (_self === document) {
                        jQuery.event.handle.call(_self, e);
                    }
                };

            jQuery(this).data(uid1, handler);

            if (_self === document) {
                /* Must be live() */
                if (_self.addEventListener) {
                    _self.addEventListener('focus', handler, true);
                } else {
                    _self.attachEvent('onfocusin', handler);
                }
            } else {
                return false;
            }

        },
        teardown: function() {
            var handler = jQuery(this).data(uid1);
            if (this === document) {
                if (this.removeEventListener) {
                    this.removeEventListener('focus', handler, true);
                } else {
                    this.detachEvent('onfocusin', handler);
                }
            }
        }
    };

    jQuery.event.special.blur = {
        setup: function() {
            var _self = this,
                handler = function(e) {
                    e = jQuery.event.fix(e);
                    e.type = 'blur';
                    if (_self === document) {
                        jQuery.event.handle.call(_self, e);
                    }
                };

            jQuery(this).data(uid2, handler);

            if (_self === document) {
                /* Must be live() */
                if (_self.addEventListener) {
                    _self.addEventListener('blur', handler, true);
                } else {
                    _self.attachEvent('onfocusout', handler);
                }
            } else {
                return false;
            }

        },
        teardown: function() {
            var handler = jQuery(this).data(uid2);
            if (this === document) {
                if (this.removeEventListener) {
                    this.removeEventListener('blur', handler, true);
                } else {
                    this.detachEvent('onfocusout', handler);
                }
            }
        }
    };

})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM