【发布时间】:2012-11-13 20:03:44
【问题描述】:
我正在用 JavaScript 编写自己的“类”函数,这样我就可以调用它,它会按照我想要的方式格式化表单。
问题是我在一个可以访问 jQuery 的网站上编写了我的 TextFade 函数,但我希望我自己的代码是独立的(不依赖于 jQuery)
我的问题是如何转换这段代码:
this.textFade = function(element,password){ // needs to be changed so jQuery is not needed anymore
if(password === 'undefined')
password = false; // default
$(element).focus(function() {
if( this.value == this.defaultValue )
this.value = "";
if(password == true)
this.type = "password";
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
if(password == true)
this.type = "text";
}
});
}
对于不需要 jQuery 的东西。
我遇到的主要问题是我无法检查元素上触发了哪些事件,如果有人可以向我解释我将能够自己修复它(如果可能,不使用其他参数来定义它) .
试图在 jQuery 中找到它,但由于某种原因我找不到该函数。
【问题讨论】:
-
使用 HTML5 的
placeholder属性(加上 polyfill to add support in older browsers)可能会更好地实现您想要做的事情。
标签: javascript forms function