$.noop () and $.map () functions

Source: Internet
Author: User

Recently found in the Project $.noop () function, because previously did not use the query, now organized as follows:

  

  jQuery.noop()A function is an empty function , and it does nothing.

When you need to pass in a function parameter at some point, and you want it to do nothing, you can use the function, and you don't need to create a new empty function.

The function belongs to the global jQuery object.

Grammar

JQuery 1.4 Adds this static function.

JQuery.  NoOp()   
return value

  jQuery.noop()The function does not have a return value (also visible as return undefined).

Example & Description

  jQuery.noop()The source code part of the function is as follows:

  NoOp:function(){}   

The following jquery sample code demonstrates jQuery.noop() the specific use of a function:

Mass transferinto an empty function as an argument, returning an empty arrayEmptyfunctionThe return value is undefined, and the null or undefined value is ignored by $.map ().VarResult= $.map   ( [1, 2, 3],< Span class= "PLN" > $. document. ( Result. Length  //0 

Note: $.map ()

JQuery Traversal-Map () method
Instance

To build a list of all values in the form:

$ ("P"). Append ($ ("input"). .map(function(){  return $(this).val();}) get (). Join (","));
Definition and usage

Map () passes each element through a function to the current matching collection, generating a new JQuery object that contains the return value.

Grammar
. Map (callback (index,domelement))
Parameters Description
Callback (Index,domelement) The function object that is called for each element in the current collection.
Detailed description

Because the return value is an array of jQuery encapsulation, use get () to process the returned object to get the underlying array.

The. Map () method is particularly useful for getting or setting the value of an element set. Consider the following form with a series of check boxes:

<form method= "POST" action= ">  <fieldset>    <div>      <label for=" both ">2</label >      <input type= "checkbox" value= "2" id= "I" Name= "number[" ">    </div>    <div>      <label for= "Four" >4</label>      <input type= "checkboxes" value= "4" id= "four" name= "number[" ">    </div>    <div>      <label for= "Six" >6</label>      <input type= "checkbox" value= "6" Id= "Six" name= "number[" ">    </div>    <div>      <label for=" eight ">8</label>      <input type= "checkbox" value= "8" id= "eight" name= "number[" ">    </div>  </fieldset> </form>

We are able to get a comma-delimited list of check box IDs:

$ (': CheckBox ') .map(function() {  return this.id;}) . Get (). Join (', ');

The result of this call is the string: "Two,four,six,eight".

Inside the callback function, this references the current DOM element for each iteration. The function can return a separate data item, or an array of data items to be inserted into the result set. If an array is returned, the elements within the array are inserted into the collection. If the function returns null or undefined, no element is inserted.

$.noop () and $.map () functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.