javascript - 从 jQuery 选择器中排除具有特定属性的元素

标签 javascript jquery css-selectors

我正在使用这个 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 : '');
        }
    });
   });

我正在寻找一种方法来排除那些通过 readonly="readonly" 设置为 readonlyinput 字段。我知道我应该使用 .not(我猜),但我不知道如何使用。

最佳答案

如果你想要所有的输入元素都是可写的(不是只读的),你可以使用下面的选择器:

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

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

[readonly="readonly"]

因为你可以同时拥有这两个:

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

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

关于javascript - 从 jQuery 选择器中排除具有特定属性的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4406259/

相关文章:

python - CSS 选择器 : select element where (parent|children) don't match X

javascript - 为什么 .toUpperCase() 不将 str[i] 大写?

javascript - Angular : $httpBackend mock only some requests

javascript - 有没有办法计算 Highcharts 事件量表上的自定义标签位置?

jquery - 利用 iPhone 的陀螺仪值来控制 HTML 元素

javascript - Rails 3 中的 jQuery 带有 "Beginning Rails 3"

jquery - jquery mobile 上的 $.ajax

html - CSS 否定伪类 :not() for parent/ancestor elements

css - 第 n 个嵌套元素的选择器

javascript - Typescript 不/不能在泛型上使用传递的类型