使用jQuery获取输入的默认值 [英] Get default value of an input using jQuery
本文介绍了使用jQuery获取输入的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$(".box_yazi2").each(function () {
var default_value = this.value;
$(this).css('color', '#555'); // this could be in the style sheet instead
$(this).focus(function () {
if (this.value == default_value) {
this.value = '';
$(this).css('color', '#000');
}
});
$(this).blur(function () {
if (this.value == '') {
$(this).css('color', '#555');
this.value = default_value;
}
});
});
这个输入的默认值函数在FF中不起作用,但在IE
并且当然输入本身看起来像这样:
This function of default value of input doesnt work in FF, but perfectly works in IE and ofcourse the input itself looks like this:
<input type="text" class="box_yazi2" id="konu" name="konu" value="Boş" />
推荐答案
解决方案非常简单;你在代码中有一个额外的});
(谢谢@ Box9)。
The solution is quite easy; you have an extra });
in your code (thanks @ Box9).
我鼓励你重用该变量并不会创建数十个jQuery对象。
I would encourage you to reuse the variable and not create dozens of jQuery objects.
我已将您的示例更改为 background-color
,但它
$ b
I've changed your example to background-color
but it will work.
$('.box_yazi2').each(function(index, element) {
var $element = $(element);
var defaultValue = $element.val();
$element.css('background-color', '#555555');
$element.focus(function() {
var actualValue = $element.val();
if (actualValue == defaultValue) {
$element.val('');
$element.css('background-color', '#3399FF');
}
});
$element.blur(function() {
var actualValue = $element.val();
if (!actualValue) {
$element.val(defaultValue);
$element.css('background-color', '#555555');
}
});
});
这篇关于使用jQuery获取输入的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文