我使用此jquery函数来显示文本框提示
这是功能
function textboxHint(id, options) {
var o = { selector: 'input:text[title]', blurClass:'blur' };
$e = $('#'+id);
$.extend(true, o, options || {});
if ($e.is(':text')) {
if (!$e.attr('title')) $e = null;
} else {
$e = $e.find(o.selector);
}
if ($e) {
$e.each(function() {
var $t = $(this);
if ($.trim($t.val()).length == 0) { $t.val($t.attr('title')); }
if ($t.val() == $t.attr('title')) {
$t.addClass(o.blurClass);
} else {
$t.removeClass(o.blurClass);
}
$t.focus(function() {
if ($.trim($t.val()) == $t.attr('title')) {
$t.val('');
$t.removeClass(o.blurClass);
}
}).blur(function() {
var val = $.trim($t.val());
if (val.length == 0 || val == $t.attr('title')) {
$t.val($t.attr('title'));
$t.addClass(o.blurClass);
}
});
// empty the text box on form submit
$(this.form).submit(function(){
if ($.trim($t.val()) == $t.attr('title')) $t.val('');
});
});
}
}
这是html
<form action="" method="" class="search" >
<input type="text" name="search" class="text" title="Search Subjects..." id="block" />
<input type="submit" name="submit" value="Search" class="submit" />
</form>
我这样调用了函数..
$(document).ready(function() {
textboxHint("block");
});
这是有效..但问题是现在我需要使用此功能在两个不同的文本框中显示两个不同的ID的提示。例如:Id = block1和Id = block2等......
有人可以帮我修改这个功能吗?
答案 0 :(得分:4)
这个怎么样:
$(document).ready(function() {
textboxHint("block1");
textboxHint("block2");
textboxHint("block3");
});
正如您所看到的,由于函数要求id作为参数,您可以通过多次调用函数来提供不同的id。
此致
答案 1 :(得分:2)
如何修改函数以接收选择器并循环遍历每个元素:
function textboxHint(selector, options) {
$(selector).each(function(){
var o = { selector: 'input:text[title]', blurClass:'blur' };
$e = this;
$.extend(true, o, options || {});
if ($e.is(':text')) {
if (!$e.attr('title')) $e = null;
} else {
$e = $e.find(o.selector);
}
if ($e) {
$e.each(function() {
var $t = $(this);
if ($.trim($t.val()).length == 0) { $t.val($t.attr('title')); }
if ($t.val() == $t.attr('title')) {
$t.addClass(o.blurClass);
} else {
$t.removeClass(o.blurClass);
}
$t.focus(function() {
if ($.trim($t.val()) == $t.attr('title')) {
$t.val('');
$t.removeClass(o.blurClass);
}
}).blur(function() {
var val = $.trim($t.val());
if (val.length == 0 || val == $t.attr('title')) {
$t.val($t.attr('title'));
$t.addClass(o.blurClass);
}
});
// empty the text box on form submit
$(this.form).submit(function(){
if ($.trim($t.val()) == $t.attr('title')) $t.val('');
});
});
}
});
}
并称之为:
<form action="" method="" class="search" >
<input type="text" name="search" class="text" title="Search Subjects..." class="block" id="block1" />
<input type="text" name="search" class="text" title="Search Subjects..." class="block" id="block2" />
<input type="submit" name="submit" value="Search" class="submit" />
</form>
$(document).ready(function() {
textboxHint(".block");
});