Custom JS Control-simple example

Source: Internet
Author: User

1. Business requirements: Make a button object and then call it like Winfrom:

First we create a new MyControls js file: (Insert the following code)

//the object-oriented idea used here is to create a new button objectvarbutton =function(ClientId) { This. Control =NULL;//Properties: The button object's own             This. ID =NULL;//the ID of the button object             This. Value =NULL;//the value displayed by the button object             This. Click =NULL;//Click events for Button Objects            //Next is the initialization, the properties of all the buttons             This. init =function () {                 This. id = ClientId;//This is the ID that the page passed over.                 This. Control = $ ("#" + ClientId);//This is the control that is bound by the ID                varbutton_html = ' <div href= ' # "id=" Button_ ' + This. id+ ' "class=" button "> ' + This. Value + ' </div> ';//This is the HTML file we want to insert.                 This. Control.after (button_html); //Binding a point-and- Click event$ ("#button_" + This. id+). Unbind ("click"). Bind ("click", This. click);  This. Control.hide ();//hide the current control            }; }

What do we do with this object next?

First, we first import the jquery plugin

And then we're going to introduce the path to this file:

Next write the style of the button:

<style type= "Text/css" >. Button{padding:10px 20px 10px 20px;Border:Medium Solid #FF00FF;width:Auto;Height:20px;float: Left;Background-color:#808080;font-family:Young Circle;font-size:Large;Font-weight:Bold; }</style>

Next, we'll write in your Web page:

<DivID= "BTN"></Div>//Here is our to change it into a button<Scripttype= "Text/javascript"> varBtn_button= Newbutton ("btn"); //here is a new button for the object, there is no feeling very like the development of WinfromBtn_button.value= "a button"; //Here's a property assignment for him.Btn_button.click= function () {   //Here is the button click eventAlert ("The show has come out ·");                 } btn_button.init (); //then initialize the button, note!!! Initialization must be performed only after all property assignments have been completed </Script>

Save, open, and run to see the results.

Custom JS Control-simple example

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.