Javascript 使用jQuery将输入框标题设置为值
我正在创建输入文本框,在加载页面时将其title属性设置为其值。当用户单击文本框时,其值将被清除,当文本框失去焦点时,用户输入的值将保留,或者默认值将取代它 问题:我似乎无法在页面加载时设置文本框的值。不过,更改“聚焦”和“模糊”的值效果很好。怎么了 jQuery代码 HTML代码Javascript 使用jQuery将输入框标题设置为值,javascript,jquery,html,css,forms,Javascript,Jquery,Html,Css,Forms,我正在创建输入文本框,在加载页面时将其title属性设置为其值。当用户单击文本框时,其值将被清除,当文本框失去焦点时,用户输入的值将保留,或者默认值将取代它 问题:我似乎无法在页面加载时设置文本框的值。不过,更改“聚焦”和“模糊”的值效果很好。怎么了 jQuery代码 HTML代码 您试图设置jQuery对象上不存在的属性(.value)。改用$(“[YourSelector]”).val() 更改此项: $(".splash_register_short_input, .splash_regi
您试图设置jQuery对象上不存在的属性(
.value
)。改用$(“[YourSelector]”).val()
更改此项:
$(".splash_register_short_input, .splash_register_long_input").value = $(this).attr('title');
$(".splash_register_short_input, .splash_register_long_input").value = $(".splash_register_short_input, .splash_register_long_input").attr('title');
为此:
$(".splash_register_short_input, .splash_register_long_input").val($(this).attr('title'));
$(".splash_register_short_input, .splash_register_long_input").val($(".splash_register_short_input, .splash_register_long_input").attr('title'));
我认为我们需要在这里枚举。每个函数都应该用于为每个文本框赋值,例如
$(".splash_register_short_input, .splash_register_long_input").each(function(){
$(this).val($(this).attr('title'));
});
类似的方法应该会奏效:
$(document).ready(function(){
$('input[type=text]').focus(function(){
if($(this).val() == $(this).attr('title'))
{
$(this).val('');
}
});
$('input[type=text]').blur(function(){
if($(this).val() == '')
{
$(this).val($(this).attr('title'));
}
});
});
但是你也可以。我是否也应该使用
$(this.val()
或this.value
但不使用$(this.value)和this.val()
?@Nyxynyx,正确此
引用HTML元素<代码>$(此)
引用jQuery对象。
$(".splash_register_short_input, .splash_register_long_input").each(function(){
$(this).val($(this).attr('title'));
});
$(document).ready(function(){
$('input[type=text]').focus(function(){
if($(this).val() == $(this).attr('title'))
{
$(this).val('');
}
});
$('input[type=text]').blur(function(){
if($(this).val() == '')
{
$(this).val($(this).attr('title'));
}
});
});