Javascript jQuery.not()方法工作不正常

Javascript jQuery.not()方法工作不正常,javascript,jquery,Javascript,Jquery,我试图让所有文本区域删除焦点上的默认文本,除了类“预填充”的文本。 但问题是“预填充”类的文本区域仍在被选择和包含 关于如何解决这个问题的建议? 谢谢 在.not方法调用中不应包含jQuery变量($)。试试这个: .not('.pre-fill') 在代码的上下文中: $('textarea, input:text, input:password').not('.pre-fill').focus(function() { if ($(this).val() === $(thi

我试图让所有文本区域删除焦点上的默认文本,除了类“预填充”的文本。 但问题是“预填充”类的文本区域仍在被选择和包含

关于如何解决这个问题的建议? 谢谢


.not
方法调用中不应包含jQuery变量(
$
)。试试这个:

.not('.pre-fill')
在代码的上下文中:

$('textarea, input:text, input:password').not('.pre-fill').focus(function() {
        if ($(this).val() === $(this).attr('defaultValue')) {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).val()==='') {
            $(this).val($(this).attr('defaultValue'))
        }
    });

.not
方法调用中不应包含jQuery变量(
$
)。试试这个:

.not('.pre-fill')
在代码的上下文中:

$('textarea, input:text, input:password').not('.pre-fill').focus(function() {
        if ($(this).val() === $(this).attr('defaultValue')) {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).val()==='') {
            $(this).val($(this).attr('defaultValue'))
        }
    });

您也可以使用

请注意,
this.value
$(this.val()
更有效,因为它是对DOM元素属性的直接访问,而不是通过首先构建jQuery对象然后调用jQuery方法来访问完全相同的属性

$('textarea, input:text:not(.pre-fill), input:password:not(.pre-fill)')
   .focus(function() {           
        if (this.value === $(this).attr('defaultValue')) {
            this.value = "";
        }
    }).blur(function() {
        if (this.value === '') {
            this.value = $(this).attr('defaultValue');
        }
    });

您也可以使用

请注意,
this.value
$(this.val()
更有效,因为它是对DOM元素属性的直接访问,而不是通过首先构建jQuery对象然后调用jQuery方法来访问完全相同的属性

$('textarea, input:text:not(.pre-fill), input:password:not(.pre-fill)')
   .focus(function() {           
        if (this.value === $(this).attr('defaultValue')) {
            this.value = "";
        }
    }).blur(function() {
        if (this.value === '') {
            this.value = $(this).attr('defaultValue');
        }
    });

this.value
$(this.val()
更有效,因为您不必先创建jQuery对象,然后调用jQuery方法。
this.value
$(this.val()
更有效,因为您不必先创建jQuery对象,然后调用jQuery方法。