JS treats strings as function execution methods

Source: Internet
Author: User

A string contains the functions and parameters to be executed and needs to be executed. Run the following command in JS:

<Script language = "JavaScript">
Function Test (STR ){
Alert (STR );
}

Window['Test'] ('aaaaaaaaaaaaaaaaaaaaaaaaa ');
</SCRIPT>

-------------------------------
Method 1...



<Script language = "JavaScript">
Function Test (STR ){
Alert (STR );
}

Eval('Test ("aaaaaaaaaaaaaaaaaaa ")');
</SCRIPT>

------------------------------------
This is method 2 ....

 

The function to be executed is set in the value of the checkbox, and the method parameter is to pass the checkbox currently clicked as the parameter.

Here I use large jquery for operations

 

$ (Function (){
$ ("Ul Li a" ).css ("cursor", "Pointer ");
$ ("Ul Li a"). Click (function (){
VaR $ ck = $ (this). siblings (": checkbox ");
If ($ CK. ATTR ("name") = "Total") // select all
{
If ($ CK. ATTR ("checked") = false ){
$ CK. ATTR ("checked", "checked ");
$ ("[Name = 'ck ']: checkbox"). ATTR ("checked", "checked ");
VaR FF = $ CK. Val (); // gets the string of the function to be executed
VaR cc = $ CK. Get (0); // converts a jquery object to a DOM object.
Window [ff] (CC); // execute the string function and pass the current checkbox object as a parameter

}
Else {
$ CK. ATTR ("checked ","");
$ ("[Name = 'ck ']: checkbox"). ATTR ("checked ","");
VaR FF = $ CK. Val ();
VaR cc = $ CK. Get (0 );
Window [ff] (CC );
}
}
Else // select one
{
If ($ CK. ATTR ("checked") = false ){
$ CK. ATTR ("checked", "checked ");
VaR S = $ CK. Val ();
VaR A = $ CK. Get (0 );
Window [s] ();
}
Else {
$ CK. ATTR ("checked ","");
VaR S = $ CK. Val ();
VaR cb = $ CK. Get (0 );
Window [s] (CB );
}
}

})
})

 

 

 

 

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.