我正在使用这个 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"
设置为 readonly
的 input
字段。我知道我应该使用 .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/