jQuery输入值切换
我一直在使用这段代码来切换焦点和模糊输入的值:
$('input, textarea').focus(function() {
value=$(this).val();
$(this).attr("value","")};
});
$('input, textarea').blur(function() {
if($(this).val()=="") {
$(this).val(value);
}
});
唯一的问题是,当我在这些输入中输入一些内容,然后在输入另一个输入后重新聚焦时,该字段会变成空白。有没有办法编辑这段代码来抑制这种情况?
这里为您提供示例 :)
我尝试了这样的方法,但它没有达到我想要的效果:
$('input, textarea').focus(function() {
value=$(this).val();
if($(this).val(value)) {
$(this).attr("value","");
});
$('input, textarea').blur(function() {
if($(this).val()=="") {
$(this).val(value);
}
});
I've been using this code to switch the value of my inputs onFocus and Blur:
$('input, textarea').focus(function() {
value=$(this).val();
$(this).attr("value","")};
});
$('input, textarea').blur(function() {
if($(this).val()=="") {
$(this).val(value);
}
});
Only problem is, when I type something into these inputs, then refocus after typing into another, the field goes blank. Is there a way to edit this code to inhibit this?
I tried something like this, but it didn't have the effect I desire:
$('input, textarea').focus(function() {
value=$(this).val();
if($(this).val(value)) {
$(this).attr("value","");
});
$('input, textarea').blur(function() {
if($(this).val()=="") {
$(this).val(value);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要设置初始值并进行检查。
演示:http:// jsfiddle.net/EMYmG/2/
更新
您可以避免存储到数据,因为我们可以使用一个名为
defaultValue
的标准属性。 (变得更加清晰)演示:http://jsfiddle.net /PjsRQ/
You need to set the initial value and check against that..
Demo: http://jsfiddle.net/EMYmG/2/
Update
You can avoid storing to the data, since there is a standard property named
defaultValue
that we can use instead. (gets much cleaner)Demo : http://jsfiddle.net/PjsRQ/
值不是标签。试图假装它们会导致严重的可访问性问题。
如果你真的想使用这种设计,我劝你不要这样做。然后将标签放置在输入下方并设置它们的样式,以便它们不可见(但仍然存在于屏幕阅读器的 DOM 中)。
jQuery 示例在我的网站上,代码很清晰,但是它正在等待正确记录。
Values are not labels. Trying to pretend they are leads to major accessibility problems.
If you really want to use this sort of design, and I urge you not to. Then position labels under the inputs and style them so they can't be seen (but still exist in the DOM for screen readers).
A jQuery example is on my website, the code is clear, but it is waiting to be properly documented.
尝试这样的事情: http://jsfiddle.net/Ajcmq/
这是一件非常简单的事情,真的:如果该字段的值是最初的四个值之一,然后清除它,否则,我们假设它已被更改,并且我们将其保留在焦点上。
唯一的缺点是,当您在另一个字段中使用四个默认值之一时,它也会被清空:D。
Try something like this: http://jsfiddle.net/Ajcmq/
It's a very simple thing, really: if the field's value is one of the initial four, then clear it, otherwise, we assume it's been changed and we leave it alone on focus.
The only drawback is that when you use one of the four dfault values in another field, it will be emptied as well :D.
jQuery 表单示例插件 执行此操作的方式是:为输入元素分配一个类,并在元素中的初始值更改后将其删除,以确定哪些元素在 DOM 加载后已被编辑以及哪些元素仍包含初始值。
The way the jQuery Form Example plugin does this is by assigning a class to the input elements and removing it after the initial value in the elements has been changed to determine which elements have been edited after the DOM load and which still contain the initial value.
为什么不使用 jQuery 提示叠加?
http://jsfiddle.net/6y2wX/
Why not use a jQuery Hint Overlay?
http://jsfiddle.net/6y2wX/