2014-03-03 35 views
2

我已實現了以下腳本來顯示在IE輸入框用於顯示的佔位符,但它不工作密碼字段,請諮詢我..佔位符沒有在IE瀏覽器的密碼字段

<script> 
jQuery(function() { 
    jQuery.support.placeholder = false; 
    test = document.createElement('input'); 
    if('placeholder' in test) jQuery.support.placeholder = true; 
    }); 

    // Placeholder for IE 
    $(function() { 
     if(!$.support.placeholder) { 

      var active = document.activeElement; 
      $(':text, textarea').focus(function() { 
       if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) { 
        $(this).val('').removeClass('hasPlaceholder'); 
       } 
      }).blur(function() { 
       if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) { 
        $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder'); 
       } 
      }); 
      $(':text, textarea').blur(); 
      $(active).focus(); 
      $('form').submit(function() { 
       $(this).find('.hasPlaceholder').each(function() { $(this).val(''); }); 
      }); 
     } 

    }); 
</script> 
+0

看起來對於佔位符來說是非常大的代碼。你可能想要瀏覽這個[post](http://stackoverflow.com/a/19899531/1577396) –

+1

@ T.J.Crowder對不起,我不知道,但我現在就學會了。 – alalp

+0

我有** IE 10 **和你的代碼正在工作... –

回答

2

嘗試添加input[type="password"]你的選擇,所以更改:

$(':text, textarea') 

到:

$(':text, textarea, input[type="password"]') 

因爲:text selector只能選擇<input type="text">元素

+0

它工作正常。 – Naju

2

兩個問題:

  1. 你沒有選擇密碼字段。 :text jQuery選擇器只會選擇input,它們是type=text(顯式或隱式)。您必須將input[type=passsword]添加到您的選擇器以包含密碼字段。

  2. 但是,一旦你解決了這個問題,你將會遇到這樣的問題,當然,密碼字段的值顯示爲objecured。因此,在其上設置佔位符文本將不會顯示佔位符。您必須以其他方式顯示佔位符文本,例如您在該字段頂部覆蓋的spandiv

相關問題