jquery Custom Extension methods

Source: Internet
Author: User

jquery is a popular JS framework, custom JS method, encapsulated into jquery, call up is also very convenient, how to write jquery extension method that, the Web read a part of the code, in fact, quite simple:

Way One:

(jquery.fn.setapdiv=function () {//Apdiv Floating Layer display position centering control        varwheight=$ (window). Height (); varWwidth=$ (window). width (); varapheight=wheight-$ ("#apDiv"). Height (); varapwidth=wwidth-$ ("#apDiv"). width (); $("#apDiv"). CSS ("Top", apheight/2); $("#apDiv"). CSS (" Left", apwidth/2); }) or: (function ($) {$.fn.extend ({//pop-up window masking layershowloaddialog:function () {//Apdiv Floating Layer display position centering control        varwheight=$ (window). Height (); varWwidth=$ (window). width (); varapheight=wheight-$ ("#apDiv"). Height (); varapwidth=wwidth-$ ("#apDiv"). width (); $("#apDiv"). CSS ("Top", apheight/2); $("#apDiv"). CSS (" Left", apwidth/2); })}) (JQuery)

Calling code:

$ ("#apDiv"). Setapdiv ();

Way two:

Jquery.extend ({//Set Apdivsetapdiv:function () {//Apdiv Floating Layer display position centering control            varwheight=$ (window). Height (); varWwidth=$ (window). width (); varapheight=wheight-$ ("#apDiv"). Height (); varapwidth=wwidth-$ ("#apDiv"). width (); $("#apDiv"). CSS ("Top", apheight/2); $("#apDiv"). CSS (" Left", apwidth/2);    }      }); Or: (function ($) {$.extend ({//pop-up window masking layershowloaddialog:function () {//Set Apdivsetapdiv:function () {//Apdiv Floating Layer display position centering control            varwheight=$ (window). Height (); varWwidth=$ (window). width (); varapheight=wheight-$ ("#apDiv"). Height (); varapwidth=wwidth-$ ("#apDiv"). width (); $("#apDiv"). CSS ("Top", apheight/2); $("#apDiv"). CSS (" Left", apwidth/2); })}) (JQuery)

Calling code:

$.setapdiv ();

Third, Expansion:

In jquery, Ajax commits, in the form of forms, we take the method of removing the input value, and then composing the JSON or other way to commit, if repeated with $ ("#vid"). Val (), this way of processing, looks very cumbersome, can not be used to send a form with other that, After reviewing the data, we found a map method to solve the problem, the code is pasted as follows:

$.ajax ({varstr_data=$ ("#dlg_form Input"). Map (function () {return($( This). attr ("name")+'='+$( This). Val ());}).Get(). Join ("&") ; Type:"POST", URL:"some.php", Data:str_data, Success:function (msg) {alert ("Data Saved:"+msg); }});

Refer to Good blog post address: http://www.cnblogs.com/nangong/archive/2013/07/23/3208302.html

jquery Custom Extension methods

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.