2010-12-10 161 views
4

我正在使用此javascript在集中時清空輸入/文本區域。從jQuery選擇器中排除具有特定屬性的元素

$(document).ready(function() { 
    $('input[type="text"],textarea').not('[readonly="readonly"]').addClass("idleField"); 
    $('input[type="text"],textarea').focus(function() { 
     $(this).removeClass("idleField").addClass("focusField"); 
     if (this.value == this.defaultValue){ 
      this.value = ''; 
     } 
     if(this.value != this.defaultValue){ 
      this.select(); 
     } 
    }); 
    $('input[type="text"],textarea').blur(function() { 
     $(this).removeClass("focusField").addClass("idleField"); 
     if ($.trim(this.value) == ''){ 
      this.value = (this.defaultValue ? this.defaultValue : ''); 
     } 
    }); 
    }); 

我正在尋找一種方法來排除那些input字段,通過readonly="readonly"設置readonly。我知道我應該使用.not(我猜),但我不知道如何。

回答

7

如果你想要的是可寫的(不是隻讀)所有輸入的元素,你可以使用以下的選擇:

$("input:not([readonly])") 

:not後的參數選擇匹配。 [readonly]將匹配任何設置readonly的內容。請注意,下列現象不會總是奏效:

[readonly="readonly"] 

正如你可以有這兩種:

<input readonly type="text"> 
<input readonly="readonly" type="text"> 

您還可以使用:而不是「輸入文本區域」僞選擇「輸入」 。請參閱文檔here

相關問題