0

我发现所有模拟 HTML5 占位符的示例都在不支持的浏览器上触发focus,这不是它在支持浏览器中的工作方式(而且有点糟糕)。触发 on(' keypress ')会更好(恕我直言),但这不适合粘贴操作,所以我想触发(或者)按键和/或粘贴事件的组合。

jQuery(input).on('keypress', function()
{/*works*/}

jQuery(input).on('paste', function()
{/*works*/}

但我不能让两者在一个功能中一起工作!例如

jQuery(input).on('keypress, paste', function()
{/*just doesn't work WTF? */}

(FWIW),这就是我所拥有的...... jsFiddle

// Add placeholder browser support as needed.
jQuery(function()
{   if(!ph_support()) alt_placeholder();
});
function alt_placeholder()
{   jQuery('input[placeholder]').each(function()
    {   //Assign to those input elements that have 'placeholder' attribute
        var input = jQuery(this);
        jQuery(input).val(input.attr('placeholder')).addClass('hasPlaceholder');
        jQuery(input).on('paste', function()
        {   input.removeClass('hasPlaceholder');
            if (input.val() == input.attr('placeholder'))
                input.val('');
        });
        jQuery(input).on('keypress', function()
        {   input.removeClass('hasPlaceholder');
            if (input.val() == input.attr('placeholder'))
                input.val('');
        });
        jQuery(input).blur(function()
        {   if (input.val() == '' || input.val() == input.attr('placeholder'))
                input.val(input.attr('placeholder')).addClass('hasPlaceholder');
        });
    });
}
function ph_support()
{   var test = document.createElement('input');
    if('placeholder' in test) return true;
    else return false;
}
4

1 回答 1

3

去掉逗号

jQuery(input).on('keypress paste', function()
于 2013-05-13T17:41:15.880 回答