Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the value function of jQuery

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use the value function of jQuery". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Val=value.call (this,i,jQuery (this). Val ()); / / [Note: for the two parameters of value, we do not have to pass values manually, but automatically pass values inside the val function.

Three usages of value function instance method

$('xxx') .val (): gets the current value attribute value (property) of the first element in the matching element collection

$('xxx') .val (value): sets the value attribute value (property) for each element in the matching set of elements

$('xxx') .val (function (index,value) {}):

When the hook function is not set or invalid, the return value of the function function (index,value) is set to the value attribute value (property) of each DOM element.

Among them, two formal parameters do not need to pass values, index refers to the index of the current DOM element in the jQuery object, and value refers to the old value attribute value of the current DOM object.

Value function source code

JQuery.fn.extend ({

Val:function (value) {

Varhooks,ret,isFunction

Elem=this [0]; / / elem is the first DOM element matched by the jQuery object

/ / when no parameter is passed in

If (! arguments.length) {

If (elem) {/ / if the jQuery matches the DOM element

/ / Hook judgment and execution: matches user-defined hook functions, executes and returns

Hooks=jQuery.valHooks [elem.type] | | / / if valHooks has a type attribute, the assignment corresponds to the hook

JQuery.valHooks [elem.nodeName.toLowerCase ()]; / / if valHooks has an attribute related to the DOM node name, the assignment corresponds to the hook

If (/ / if the hook and related methods are defined, call the hook function to process and return

Hooks&&

"get" inhooks&&

(ret=hooks.get (elem, "value"))! = undefined

) {

Returnret

}

When ret=elem.value;// has no hook, take the value of the element directly

Returntypeofret=== "string"?

/ / return string: handle the return value in general

Ret.replace (rreturn, ""):

/ / returns non-string: value is null/undefined, empty string is returned; in other cases, ret is returned.

Ret==null? ": ret

}

Return;// if the jQuery object does not match the DOM element, return directly

}

/ / what happens when a parameter is passed: function, non-function

This is the end of the content of "how to use jQuery's value function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report