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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:24:12  来源:igfitidea点击:

jquery function val() is not equivalent to "$(this).value="?

jquery

提问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 valfunction in jQuery?

为什么?有什么不同?valjQuery 函数背后的机制是什么?

$(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 valueproperty of the jQuery object that wraps this-- not the valueof thisitself.

有了$(this).value = ''你作为分配一个空字符串value,它包装jQuery对象的属性this-不是valuethis本身。

回答by Nathan Taylor

$(this).valueis 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 对象,请按照下列步骤操作:

  1. Download the full source code (not minified) i.e: example http://code.jquery.com/jquery-1.11.1.js.

  2. Insert Line after L96 , add this code value:""to init this new prop enter image description here

  3. Search on jQuery.fn.init, it will be almost Line 2747

  1. 下载完整的源代码(未缩小),即:示例http://code.jquery.com/jquery-1.11.1.js

  2. 在 L96 之后插入 Line ,添加此代码value:""以初始化此新道具 enter image description here

  3. 继续搜索jQuery.fn.init,几乎是第2747行

enter image description here

enter image description here

  1. Now , assign a value to valueprop : (Before return statment add this.value=jQuery(selector).val()) enter image description here
  1. 现在,为valueprop赋值:(在 return 语句 add 之前this.value=jQuery(selector).val()enter image description here

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 函数。