【问题标题】:Exclude an element with a specific attribute from jQuery selectors从 jQuery 选择器中排除具有特定属性的元素
【发布时间】:2010-12-10 14:52:04
【问题描述】:

我正在使用这个 javascript 在聚焦时清空输入/文本区域。

   $(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(我猜),但我不知道如何使用。

【问题讨论】:

    标签: javascript jquery css-selectors


    【解决方案1】:

    如果您希望所有输入元素都是可写的(不是只读的),您可以使用以下选择器:

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

    :not 后面的参数是要匹配的选择器。 [readonly] 将匹配设置了 readonly 的任何内容。请注意,以下内容并不总是有效:

    [readonly="readonly"]
    

    你可以同时拥有这两个:

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

    您也可以使用“:input”伪选择器代替“input,textarea”。请参阅文档here

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多