这是我开始的代码.问题是,在用户从文本输入中聚焦后,我想重置文本输入.我的意思是用户进入该领域,我想删除它.
$(function(){
var field = $('#searchField');
field.focusin(function(){
console.log('element is focused.');
field.animate({width: '100px'});
});
field.focusout(function(){
console.log('element is not focused.');
field.animate({width: '50px'});
field.value = ''; // this is not working.
});
});
Run Code Online (Sandbox Code Playgroud)
HTML代码:
<p><input id="searchField" type="text" width="50px"/></p>
Run Code Online (Sandbox Code Playgroud)
您正在尝试使用jQuery对象value的javascript DOM对象的属性.您需要使用jQuery val()来获取jQuery对象的值或将jquery对象转换为要使用的DOM对象value.
field.value = ''
Run Code Online (Sandbox Code Playgroud)
至
field.val(''); //using jQuery object
Run Code Online (Sandbox Code Playgroud)
要么
field[0].value = ''; //using javascript object
Run Code Online (Sandbox Code Playgroud)