jQuery onfocus onblur问题
我在让 onFocus
和 onBlur
事件正常工作时遇到一些问题
这是我得到的
var var1
$("input").focus(function() {
var1 = $(this).val()
if ( $(this).val()==var1) {
$(this).val('').css({'color': "#000", 'font-style': 'normal', 'font-weight': 'bold'});
}
$(this).css({'background-color':'#d7df23' });
});
$("input").blur(function() {
if ( $(this).attr("value")=="") {
$(this).val(var1).css({'color': "#666", 'font-style': 'italic', 'font-weight': 'normal'});
}
$(this).css({'background-color':'#EEEEEE' });
});
这是我的 HTML
<input type="text" id="tbTitle" value="Title">
<input type="text" id="tbTitle1" value="Title1">
<input type="text" id="tbTitle2" value="Title2">
如果您不更改文本框的值,这将起作用。
基本上我想获取输入的原始值并将其存储在 var 中,然后如果该字段为空则将原始值放回。
目前,它正在将文本框中的任何值放在焦点上。
有没有办法存储原始值并仅将其更改为 onBlur ?
这是我的 jsfiddle 的链接
I'm having some trouble getting my onFocus
and onBlur
events to work properly
Here's what I got
var var1
$("input").focus(function() {
var1 = $(this).val()
if ( $(this).val()==var1) {
$(this).val('').css({'color': "#000", 'font-style': 'normal', 'font-weight': 'bold'});
}
$(this).css({'background-color':'#d7df23' });
});
$("input").blur(function() {
if ( $(this).attr("value")=="") {
$(this).val(var1).css({'color': "#666", 'font-style': 'italic', 'font-weight': 'normal'});
}
$(this).css({'background-color':'#EEEEEE' });
});
And here's my HTML
<input type="text" id="tbTitle" value="Title">
<input type="text" id="tbTitle1" value="Title1">
<input type="text" id="tbTitle2" value="Title2">
This works if you don't change the value of the textbox.
Basically I want to get the original value of the input and store it in a var and then if the field is blank put the original value back.
At the moment it is putting the value of whatever is in the textbox on focus
Is there a way to store the original value and only change it to that onBlur
?
Here's a link to my jsfiddle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我的解决方案...
http://jsfiddle.net/Sohnee/YTTD5/4/
或者在代码中...
Here is my solution...
http://jsfiddle.net/Sohnee/YTTD5/4/
Or in code...
尝试设置
var var1 = null;
初始,然后当焦点触发时只需第一次设置它,如var1 = var1 ?? $(this).val();
看起来您每次都不断覆盖该值,也可能需要考虑将原始值存储为实际对象上的数据而不是全局变量try setting
var var1 = null;
initialy, then when the focus triggers just set it the very first time likevar1 = var1 ?? $(this).val();
it looks like you keep overrriding the value each time, also might want to consider storring the original value as data on the actual object instead of global variable您使用的是单个
var1
,但有多个文本框。因此,当您离开文本框时,var1
将会因您在另一个文本框中输入内容而受到重击。另外,在测试元素的值之前,您会先分配给var1
,因此==
将始终为 true。要“获取输入的原始值并将其存储在 var 中,然后如果该字段为空,则将原始值放回”,您可以执行以下操作:
我省略了上面的 CSS 内容,因为它不太完整让我清楚你想用它做什么。添加回来很容易。
You're using a single
var1
, but multiple text boxes. So thevar1
will be bludgeoned by your entry into another text box when you leave a text box. Also, you're assigning tovar1
before you test the value of the element against it, so the==
will always be true.To " get the original value of the input and store it in a var and then if the field is blank put the original value back" you could do something like this:
I've left out the CSS stuff above because it wasn't quite clear to me what you wanted to do with it. It's easy enough to add back.
没有 CS 的东西..如果我们输入一个字段,该字段的值返回到“rel”属性中的值。
退出字段时,如果该值为空,则复制 rel 属性中可能存在的任何内容,该属性要么什么也没有,要么是初始焦点上存在的值。
sans the CS stuff.. if we enter a field that has a value back that value up in a 'rel' attribute.
on exit of the field,if the value is empty, copy in whatever might be in the rel attribute, which is either nothing, or is the value that was present on the initial focus.
您的问题是您在聚焦时存储输入值,而不是在初始设置时存储输入值。
这段代码可能有用:
You problem is that you storing input value at the moment of focus, not at the moment of it's initial setting.
This piece of code may be useful: