jquery 函数 val() 不等价于 "$(this).value="?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4524702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery function val() is not equivalent to "$(this).value="?
提问by lkahtz
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('')
.
当我尝试使用 将文本输入设置为空白(单击时)时$(this).value=""
,这不起作用。我必须使用$(this).val('')
.
Why? What is the difference? What is the mechanism behind the val
function in jQuery?
为什么?有什么不同?val
jQuery 函数背后的机制是什么?
$(document).ready(function() {
$('#user_name').focus(function(){
$(this).val('');
});
});
// error code: not working...
$(document).ready(function() {
$('#user_name').focus(function(){
$(this).value='';
});
});
回答by Ken Redler
You want:
你要:
this.value = ''; // straight JS, no jQuery
or
或者
$(this).val(''); // jQuery
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.
有了$(this).value = ''
你作为分配一个空字符串value
,它包装jQuery对象的属性this
-不是value
的this
本身。
回答by Nathan Taylor
$(this).value
is attempting to call the 'value' property of a jQuery object, which does not exist. Native JavaScript doeshave a 'value' property on certain HTML objects, but if you are operating on a jQuery object you must access the value by calling $(this).val()
.
$(this).value
正在尝试调用不存在的 jQuery 对象的“值”属性。本机 JavaScript在某些 HTML 对象上确实有一个“值”属性,但如果您正在对 jQuery 对象进行操作,则必须通过调用$(this).val()
.
回答by Abdennour TOUMI
Note that :
注意 :
typeof $(this)
is JQueryobject.
typeof $(this)
是JQuery对象。
and
和
typeof $(this)[0]
is HTMLElementobject
typeof $(this)[0]
是HTMLElement对象
then :
if you want to apply .val()
on HTMLElement , you can add this extension .
那么:如果你想应用.val()
在 HTMLElement 上,你可以添加这个扩展。
HTMLElement.prototype.val=function(v){
if(typeof v!=='undefined'){this.value=v;return this;}
else{return this.value}
}
Then :
然后 :
document.getElementById('myDiv').val() ==== $('#myDiv').val()
And
和
document.getElementById('myDiv').val('newVal') ==== $('#myDiv').val('newVal')
?????
INVERSE :
?????
反转:
Conversely? if you want to add value property to jQuery object , follow those steps :
反过来?如果要将 value 属性添加到 jQuery 对象,请按照下列步骤操作:
Download the full source code (not minified) i.e: example http://code.jquery.com/jquery-1.11.1.js.
Insert Line after L96 , add this code
value:""
to init this new propSearch on
jQuery.fn.init
, it will be almost Line 2747
下载完整的源代码(未缩小),即:示例http://code.jquery.com/jquery-1.11.1.js。
在 L96 之后插入 Line ,添加此代码
value:""
以初始化此新道具继续搜索
jQuery.fn.init
,几乎是第2747行
- Now , assign a value to
value
prop : (Before return statment addthis.value=jQuery(selector).val()
)
- 现在,为
value
prop赋值:(在 return 语句 add 之前this.value=jQuery(selector).val()
)
Enjoy now : $('#myDiv').value
现在享受:$('#myDiv').value
回答by Adam F
One thing you can do is this:
你可以做的一件事是:
$(this)[0].value = "Something";
This allows jQuery to return the javascript object for that element, and you can bypass jQuery Functions.
这允许 jQuery 返回该元素的 javascript 对象,并且您可以绕过 jQuery 函数。