2013-05-13 49 views
0

所有的例子我發現模仿HTML5佔位符不支持的瀏覽器觸發重點,這是它是如何工作的支持的瀏覽器(還挺吸收)。 (IMHO)可以觸發('按鍵'),但這不適用於粘貼操作,所以我想觸發(或者)按鍵和/或粘貼事件的組合。多個事件,在jQuery.on處理一個功能

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

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

但我不能讓這兩個在一個單一的功能一起工作!例如

​​

(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; 
} 
+0

複製後http://stackoverflow.com/questions/9045692/jquery-on-the-first-time-doesnt-grab-or-pass-the-value – Ligth 2013-05-13 17:42:07

+0

這篇文章可能有點相關,但它不是重複的。 – 2013-05-13 17:45:06

回答

3

刪除逗號

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

Geesh!有時你無法看到森林的樹木! Duhhh ...謝謝! – WallabyKid 2013-05-13 17:49:23