2

我使用 jquery.hint.js。在页面加载时很好,但是当我使用后退浏览器按钮时,焦点不会隐藏提示。

jQuery.fn.hint = function (blurClass) {
    if (!blurClass) {
        blurClass = 'blur';
    }

    return this.each(function () {
        // get jQuery version of 'this'
        var $input = jQuery(this),

        // capture the rest of the variable to allow for reuse
          title = $input.attr('title'),
          $form = jQuery(this.form),
          $win = jQuery(window);

        function remove() {
            if ($input.val() === title && $input.hasClass(blurClass)) {
                $input.val('').removeClass(blurClass);
            }
        }

        // only apply logic if the element has the attribute
        if (title) {
            // on blur, set value to title attr if text is blank
            $input.blur(function () {
                if (this.value === '') {
                    $input.val(title).addClass(blurClass);
                }
            }).focus(remove).blur(); // now change all inputs to title

            // clear the pre-defined text when form is submitted
            $form.submit(remove);
            $win.unload(remove); // handles Firefox's autocomplete
        }
    });
};

正常页面加载示例:http ://prntscr.com/sik0d 使用后退浏览器按钮后示例:http: //prntscr.com/sikap(不隐藏焦点提示,它只是在输入字段中添加文本)

如何解决这个问题。如何强制此脚本在后退按钮上重新加载?谢谢

4

1 回答 1

1

我终于发现 jquery.hint.js 女巫包含使用浏览器后退按钮的修复程序。所以这里有一个链接:https ://gist.github.com/madmanlear/1723896

JavaScript:

(function ($) {

$.fn.hint = function (blurClass) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        var $input = $(this),
            title = $input.attr('placeholder'),
            $form = $(this.form),
            $win = $(window);

        function remove() {
            if ($input.val() === title) {
                $input.val('').removeClass(blurClass);
            }
        }

        // only apply logic if the element has the attribute
        if (title) { 
            // on blur, set value to title attr if text is blank

            $input.blur(function () {
                if (this.value === '' || this.value == title) {
                    $input.val(title).addClass(blurClass);
                }
            }).focus(remove).blur(); // now change all inputs to title

            // clear the pre-defined text when form is submitted
            $form.submit(remove);
            $win.unload(remove); // handles Firefox's autocomplete
        }
    });
};

})(jQuery);
于 2013-02-26T13:15:18.073 回答