Compile a framework similar to jquery $ ("ID"). Val ()

Source: Internet
Author: User

Recently, jquery has been used, and I have always wanted to write a framework similar to myself. You can click it like jquery.

In jquery, the value of the control is obtained in this way, $ ("# D "). val (), and I want to implement this function here. $ ("ID "). val ();

Code
VaR CZF = {
$: Function (ID ){
This [0] = Document. getelementbyid (ID); // This is the key to get the control object
Return this; // return the CZF object
},

// CZF. ID ('dd'). Val ();


VAL: function (){
Return this [0]. value;

}
};

CZF. _ namespace = true; // In vs 2008, you can add a smart prompt.

VaR CZF ={$: function (ID) {}, VAL: function () {}} is written in JSON,

The key point is this [0] = Document. getelementbyid (ID );
Then return this; when returning the obtained control to the CZF object, you can use CZF. $ ("ID"). Val ();

What I get with CZF. $ ("testid") is actually the CZF object, which has an attribute and two methods.

HTML page

 

Code
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> CZF implements the $ ("ID"). Val () function similar to jquery </title>
<SCRIPT type = "text/JavaScript" src = "ic. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "CZF. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
/** // * Function testclick (){
VaR testinput = IC. getelementsbyclassname ("testme", "input ");
For (VAR I = 0; I <testinput. length; I ++ ){
Alert (testinput [I]. value );
}
}*/

Function FN (){
Alert (CZF. $ ('testid'). Val ());
}

</SCRIPT>
</Head>

<Body>

<Input type = "text" value = "I am CZF" class = "testme" id = "testid"/>
<Input type = "text" value = "test3" class = "testme" id = "testid2"/>
<Input type = "button" value = "clickme" onclick = "FN ();"/>
</Body>
</Html>

You can expand other functions by yourself.

Share a tips. In vs 2008, add
CZF. _ namespace = true; // In vs 2008, two Slide lines can be displayed intelligently.

CZF. _ namespace = true; // In vs 2008, you can add a smart prompt. Here are two Slide lines __

 

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.