2013-08-28 103 views
7

佔位符不能在IE-9中工作,所以我使用下面的代碼來佔位符。佔位符不起作用

jQuery(function() { 
    debugger; 
    jQuery.support.placeholder = false; 
    test = document.createElement('input'); 
    if ('placeholder' in test) jQuery.support.placeholder = true; 
}); 
// This adds placeholder support to browsers that wouldn't otherwise support it. 
$(function() { 

    if (!$.support.placeholder) { 
     var active = document.activeElement; 
     $(':text').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').blur(); 
     $(active).focus(); 
     $('form:eq(0)').submit(function() { 
      $(':text.hasPlaceholder').val(''); 
     }); 
    } 
}); 

當我服用測試的價值,它表明我null.How可以得到所有輸入標籤的細節?

+3

佔位符屬性將不支持低於IE 10 –

+3

@Deepu貌似OP知道,並試圖實現一種變通方法... – Teemu

+0

@Deepu OP的代碼是「佔位符」支持的一個墊腳... –

回答

1

我認爲這將有助於你

if ($.browser.msie) { 
        $("input").each(function() { 
         if (IsNull($(this).val()) && $(this).attr("placeholder") != "") { 
          $(this).val($(this).attr("placeholder")).addClass('hasPlaceHolder'); 
          $(this).keypress(function() { 
           if ($(this).hasClass('hasPlaceHolder')) $(this).val("").removeClass('hasPlaceHolder'); 
          }); 
          $(this).blur(function() { 
           if ($(this).val() == "") $(this).val($(this).attr("placeholder")).addClass('hasPlaceHolder'); 
          }); 
         } 
        }); 
       } 
-3

HTML:

<input type='text' id='your_field' value='Enter value'/> 

的jQuery:

$(document).ready(function(){ 
    $("#your_field").on('focusout',function(){ 
     if($("#your_field").val() == ''){ 
      $("#your_field").val('Enter value'); 
     } 
    }); 
    $("#your_field").on('focus',function(){ 
     if($("#your_field").val() == 'Enter value'){ 
      $("#your_field").val(''); 
     } 
    }); 
}); 

DEMO

還要檢查時的形式發佈,因爲如果用戶提交表單,而無需輸入字段,則Enter value會作爲字段的值發佈。因此,在提交表單時,請在客戶端進行驗證或在服務器端進行檢查。

+0

downvotters可以檢查演示。 –

+0

我輸入「輸入值」並單擊文本框,我的文本消失。 – Doorknob

+6

這種方法的可怕之處在於,如果用戶提交表單時未修改假佔位符值,則它將使用默認數據提交它,而不像佔位符將提交空值。你應該指出,因爲新手可能會使用你的100%完全有效的代碼,但仍然無法弄清楚爲什麼佔位符文本有時會發布在他們的表單上! –

0

我在我的手機,所以這是很難,但真的是你需要做的

JQuery.support.placeholder = typeof 'placeholder' in test !== 'undefined' 

因爲空意味着沒有任何佔位符值,但有佔位符支持

從我理解你的意思是說測試中的佔位符返回null

-1

我建議你不要自己寫這個,而是尋求一個現成的解決方案。如果您只想爲舊版瀏覽器提供支持,那麼您可能需要解決自己的複雜問題。

例如,這裏是我使用的墊片(和建議上http://html5please.com):https://github.com/mathiasbynens/jquery-placeholder/blob/master/jquery.placeholder.js

來吧,閱讀代碼。這些都是你寫這樣的墊片時,需要心中有一個問題:

  • 檢測瀏覽器的支持,
  • 跟蹤時,框包含實際輸入與否;
  • 提交表單前添加一個類,允許佔位符不同的文字顏色,
  • 明確的佔位符,
  • 明確重新加載頁面時,佔位符,
  • 手柄textarea
  • 手柄input[type=password]

而這可能不是全部。 (我已鏈接庫還掛鉤到jQuery的,爲了使.val()回報''時,有在框中沒有真正的輸入


也有一個使用完全不同的方法的另一個墊片:https://github.com/parndt/jquery-html5-placeholder-shim/blob/master/jquery.html5-placeholder-shim.js

這個庫不會觸及輸入的實際值,而是直接在其上顯示一個元素。