jQuery函数val()不等于"$(this).value ="? [英] jquery function val() is not equivalent to "$(this).value="?
本文介绍了jQuery函数val()不等于"$(this).value ="?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试使用$(this).value=""
将文本输入设置为空白(单击时)时,这将不起作用.我必须使用$(this).val('')
.
When I try to set a text input to blank (when clicked) using $(this).value=""
, this does not work. I have to use $(this).val('')
.
为什么?有什么区别? jQuery val
函数背后的机制是什么?
Why? What is the difference? What is the mechanism behind the val
function in jQuery?
$(document).ready(function() {
$('#user_name').focus(function(){
$(this).val('');
});
});
// error code: not working...
$(document).ready(function() {
$('#user_name').focus(function(){
$(this).value='';
});
});
推荐答案
您要:
this.value = ''; // straight JS, no jQuery
或
$(this).val(''); // jQuery
使用$(this).value = ''
,您要为包装this
的jQuery对象的value
属性分配一个空字符串,而不是this
本身的value
.
With $(this).value = ''
you're assigning an empty string as the value
property of the jQuery object that wraps this
-- not the value
of this
itself.
这篇关于jQuery函数val()不等于"$(this).value ="?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文