ATTR('设置defaultValue')将返回使用jQuery 1.6.3未定义 [英] attr('defaultValue') is returning undefined using jQuery 1.6.3

查看:508
本文介绍了ATTR('设置defaultValue')将返回使用jQuery 1.6.3未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery的一个简单的脚本,使用jQuery 1.5.2完美的作品,你可以在此的jsfiddle 。什么是应该发生的是,当你把焦点转移到文本字段,默认值被删除。而当你离开领域的空白,原来的默认值放回原位。

I have a simple script in jQuery that works perfectly with jQuery 1.5.2 as you can see in this jsFiddle. What is supposed to happen is that when you bring focus to the text field, the default value is removed. And when if you leave the field blank, the original default value is put back in place.

http://jsfiddle.net/kHBsD/

不过,完全相同的code,其中只有jQuery的1.6.3被代替,不工作。 (不工作的手段,默认值保持在文本框,直到你手动删除它,你可以在本的jsfiddle 看到的。

However, the same exact code, where only jQuery 1.6.3 is used instead, is not working. (Not working means that the default value remains in the text box until you manually delete it as you can see in this jsFiddle.

http://jsfiddle.net/kHBsD/1/

有在控制台没有脚本误差和功能的其它方面的操作。你可以看到悬停()部分在两个jsFiddles正常工作。

There are no script errors in the console and other aspects of the function are operational. You can see the hover() portion is working fine in both jsFiddles.

摘要版(问题的根源)

jQuery的1.6.3将返回未定义 .attr('设置defaultValue')

jQuery 1.6.3 is returning undefined for .attr('defaultValue').

的jsfiddle(不工作)

然而,jQuery的1.5.2正在恢复为 .attr预期值('设置defaultValue')

However, jQuery 1.5.2 is returning the expected value for .attr('defaultValue').

的jsfiddle(工作)

问:

有谁知道为什么会是这样吗? (它看起来像一个jQuery的错误给我。)

Does anyone know why this would be happening? (It looks like a jQuery bug to me.)

以下是仍在工作......

The following is still working...

document.getElementById().defaultValue

...但我认为这是pretty丑陋不得不这样做,凡jQuery是可用的。

...but I think it's pretty ugly to have to do that where jQuery is available.

我接受其他的建议。

推荐答案

使用道具()

$( '#q' ).prop( 'defaultValue' )

现场演示: http://jsfiddle.net/kHBsD/8/

您看,设置defaultValue'不是内容属性(HTML属性),你可以看到自己,如果你看看你的HTML源$ C ​​$ C。相反,它的一个属性的 HTMLInputElement 的DOM元素节点。

You see, 'defaultValue' is not a content attribute (HTML attribute) as you can see for yourself if you look at your HTML source code. Instead, it's a property of the HTMLInputElement DOM element node.

在这里看到:<一href=\"https://developer.mozilla.org/en/DOM/HTMLInputElement\">https://developer.mozilla.org/en/DOM/HTMLInputElement

属性在HTML源$ C ​​$ C存在。结果
属性在DOM树存在。

Attributes exist in the HTML source code.
Properties exist in the DOM tree.

当浏览器解析HTML源代码code,在HTML &LT;输入&GT; 元素是PTED间$ P $和一个相应的 HTMLInputElement 创建DOM节点。这个DOM元素包含几十个属性(设置defaultValue'是其中之一)。

When the browser parses the HTML source code, the HTML <input> element is interpreted and a corresponding HTMLInputElement DOM node is created. This DOM element contains dozens of properties ('defaultValue' being one of them).

在这里,我已经重构你的code:

Here, I've refactored your code:

$( '.autoclear' ).
    focus( function () {
        if ( this.value === this.defaultValue ) {
            this.value = '';
            $( this ).removeClass( 'blur' ).addClass( 'focus' );
        }
    }).
    blur( function () {
        if ( this.value === '' ) { 
            this.value = this.defaultValue;
            $( this ).removeClass( 'focus' ).addClass( 'blur' );
        }
    });

现场演示: http://jsfiddle.net/kHBsD/9/

道具()是没有必要的 - 你有这个引用,所以你可以直接访问属性。也不需要那些悬停处理程序,只需要使用输入:悬停 CSS规则

prop() is not necessary - you have the this reference, so you can just access the property directly. Also those hover handlers are not needed, just use a input:hover CSS rule.

这篇关于ATTR('设置defaultValue')将返回使用jQuery 1.6.3未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆