2010-12-16 77 views
1

這是我的代碼..但我無法理解這段代碼。 plz幫助我你可以解釋下面的代碼

$('.maxlength') 

    .after("<span></span>") 

    .next() 

    .hide() 

    .end() 

    .keypress(function(e) { 

     var current = $(this).val().length; 

     if (current >= 130) { 

      if (e.which != 0 && e.which != 8) { 

       e.preventDefault(); 

      } 

     } 

     $(this).next().show().text(130 - current); 

    }); 
+0

順便說一句,這是一個可怕的方式來實現這一點。不要使用這段代碼! – 2010-12-16 04:13:00

+1

@paxdiablo:我的代碼意味着不是由我寫的..如果我知道這我該如何尋求幫助..如果可能使幫助不做評論..對不起,謝謝 – Mihir 2010-12-16 04:18:32

+0

我同意你,很多次我們工作代碼已經由其他人開發... – kobe 2010-12-16 04:40:37

回答

6
$('.maxlength') // select all items with class 'maxlength' 

.after("<span></span>") // insert a span after 

.next() // move to the span 

.hide() // hide the span 

.end() // go back to originally selected element 

.keypress(function(e) { // add a keypress event handler function 

    var current = $(this).val().length; // get the length of the input value (a string) 

    if (current >= 130) { //if it's long 

     if (e.which != 0 && e.which != 8) { // and if certain keys weren't pressed (delete?) 

      e.preventDefault(); // don't do what those keys would normally do - i.e. ignore the keypress 

     } 

    } 

    $(this).next().show().text(130 - current); // show the remaining chars in the newly added span 

}); 

...所以基本上這個代碼使文本區域有130個字符的限制,並顯示您所允許多少打字。

+0

非常感謝兄弟..所以這裏end()意味着停止當前的元素,並去其前一個權利!當我們使用end()時,它會轉到上一個還是原始的? – Mihir 2010-12-16 04:11:46

+0

@Mihir:http://api.jquery.com/end/:*結束當前鏈中最新的過濾操作,並將匹配元素集返回到之前的狀態。* – 2010-12-16 04:12:32

+0

非常感謝 – Mihir 2010-12-16 04:24:22

相關問題