jQuery:我用什么监听器来检查浏览器自动填写密码输入字段?

前端之家收集整理的这篇文章主要介绍了jQuery:我用什么监听器来检查浏览器自动填写密码输入字段?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的问题,我似乎无法找到解决方案.

基本上这个网站:http://dev.supply.net.nz/vendorapp/(目前正在开发中)
我有一些花哨的标签动画在焦点上放入和缩小内容.模糊.

但是,一旦用户登录,浏览器很可能会记住与用户电子邮件地址/登录相关联的密码. (哪个好,不应该被禁用.)

但是,当浏览器自动设置#password字段上的值时,我遇到触发标签滑出动画的问题,因为此事件既不是焦点也不是模糊.

当浏览器“自动填写”用户密码时,是否有人知道使用哪个监听器来运行我的功能

以下是该问题的快速屏幕截图:

Password Autofill Label Issue http://dl.dropbox.com/u/376243/fill_issue.png

解决方法

我最近读了一篇名为 Capturing AutoFill as a Change Event文章,可能就是你想要的.作者创建了一个名为listenForChange()的函数,该函数监视表单自动填充活动(自动填充甚至是单词?).由于它经常检查您的表单,我个人建议您只运行一定次数.毕竟表单自动填充通常会在页面加载完成后立即执行.

引用原文:

The plugin makes use of the trigger() and data() functions. In a nutshell,
we loop over the input element or set
of children input elements,storing
their initial value in the data cache
provided by the data() function. We
then check to see if the stored value
matches the value of the input during
the current iteration. If so,we do
nothing,if not,we manually fire the
change event via trigger(). There’s
also a bit of logic in there to ignore
the element that has focus. We don’t
need to worry about this element,
since if the value is changed while
the user has focus,the change event
will be fired as normal when the
element is blurred.

这是函数本身,因为你不想去阅读文章(你应该):

(function($) {
    $.fn.listenForChange = function(options) {
        settings = $.extend({
            interval: 200 // in microseconds
        },options);

        var jquery_object = this;
        var current_focus = null;

        jquery_object.filter(":input").add(":input",jquery_object).focus( function() {
            current_focus = this;
        }).blur( function() {
            current_focus = null;
        });

        setInterval(function() {
            // allow
            jquery_object.filter(":input").add(":input",jquery_object).each(function() {
                // set data cache on element to input value if not yet set
                if ($(this).data('change_listener') == undefined) {
                    $(this).data('change_listener',$(this).val());
                    return;
                }
                // return if the value matches the cache
                if ($(this).data('change_listener') == $(this).val()) {
                    return;
                }
                // ignore if element is in focus (since change event will fire on blur)
                if (this == current_focus) {
                    return;
                }
                // if we make it here,manually fire the change event and set the new value
                $(this).trigger('change');
                $(this).data('change_listener',$(this).val());
            });
        },settings.interval);
        return this;
    };
})(jQuery);

所有的功劳归于FurryBrains.com的所有者撰写文章.

原文链接:https://www.f2er.com/jquery/180857.html

猜你在找的jQuery相关文章