【问题标题】:multiple events, one function in jQuery.on handler多个事件,jQuery.on 处理程序中的一个函数
【发布时间】:2013-05-07 20:00:46
【问题描述】:

我发现的所有模拟 HTML5 占位符的示例都在 focus 上触发,这不是它在支持浏览器中的工作方式(有点烂)。触发('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;
}

【问题讨论】:

标签: jquery html placeholder jquery-on


【解决方案1】:

去掉逗号

jQuery(input).on('keypress paste', function()

【讨论】:

  • 天哪!有时你只是看不到森林的树木!呃……谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2013-01-18
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多