您可以在同一个 onBlur 事件上创建多个函数吗?
我正在进行一个很好的默认值检查,客户希望向 onBlur
事件添加附加功能。默认值检查是全局的并且工作正常。新功能需要将一些特定于字段的值传递给新函数。如果不是这样,我只需将其添加到默认检查中即可完成。
我的问题是我可以做这种事情并让它们都着火吗?什么是更好的方法?
默认检查
$(document.body).getElements('input[type=text],textarea,tel,email').addEvents ({
'focus' : function(){
if (this.get('value') == this.defaultValue)
{
this.set('value', '');
}
},
'blur' : function(){
if (this.get('value') == '')
{
this.set('value', (this.defaultValue));
}
}
});
添加的功能
想法是照常处理默认值,但在输入字段值时触发新函数。在我看来,必须先开火,但怎么开火呢?
同样,因为值传递了特定于字段的值,所以我不能将其转储到全局default-check
函数中。
我希望这是有道理的。
I have a nice default-value check going and client wants to add additional functionality to the onBlur
event. The default-value check is global and works fine. The new functionality requires some field-specific values be passed to the new function. If it weren't for this, I'd just tack it onto the default-check and be done.
My question is can I do this sort of thing and have them both fire? What would be a better way?
THE DEFAULT-CHECK
$(document.body).getElements('input[type=text],textarea,tel,email').addEvents ({
'focus' : function(){
if (this.get('value') == this.defaultValue)
{
this.set('value', '');
}
},
'blur' : function(){
if (this.get('value') == '')
{
this.set('value', (this.defaultValue));
}
}
});
ADDED FUNCTIONALITY
<input type='text' id='field_01' value='default data' onBlur='driveData(event, variableForThisField, this.value);'>
The idea is to handle default values as usual, but fire the new function when the field's values are entered. Seems to me one has to fire first, but how?
Again, because the values passed a field specific, I can't just dump it into the global default-check
function.
I hope this makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将自定义变量存储在自定义数据属性中即可。技术上不符合 HTML 4 标准,但它可以工作:
fiddle: http://jsfiddle.net/QkcxP /6/
Just store the custom variable in a custom data attribute. Not technically standards-compliant [in HTML 4], but it works:
fiddle: http://jsfiddle.net/QkcxP/6/
我想你正在使用Mootools?我不太认识语法,但我创建了一个 JSFiddle,它似乎可以与 Mootools 一起使用,而不是其他库。
这是我创建来测试它的小提琴: http://jsfiddle.net/NDTsz/
答案似乎是是 - 当然是在 Chrome 中,但我相信每个浏览器。
I think you're using Mootools? I don't quite recognize the syntax, but I created a JSFiddle and it seems to work with Mootools, and not other libraries.
Here's the fiddle I created to test it: http://jsfiddle.net/NDTsz/
Answer would appear to be yes - certainly in Chrome, but I believe in every browser.