Some common methods of JS Summary _ basic knowledge

Source: Internet
Author: User
Tags ming

Val (), append (), get (), Split (), substr (), each (), HTML (), KeyUp (), Trim (), show (), Hide (), indexOf ()
One by one:
Val (): To take a value and assign a page element
Value: var result = $ ("#txtSearch"). Val ();
Assignment: $ ("#txtSearch"). Val (result);

Each (): the operation of a set, followed by a method call to each element within the collection, for example:
$.each (data.list, function (I, item) {
Alert (item["Wikititle"] "+" item["wikiid");//i is the element of the collection. The following table, item represents the element itself
});

Append (): Append elements after the specified page element
For example, a list element <li>test!</li&gt, a page element like this can dynamically add multiple <li> items through append methods such as:
The page has a id= "div_keycontent" div: <div id= "Div_keycontent" ></div>
JS inside can be written like this:
$.each (data.list, function (I, item) {
$ ("#div_keycontent"). Append ("<li>" + item["Wikititle"] + "<a href=\" read/"+ item[" wikiid "] +" \ "> View </a& gt; "+" </li> ");
});

Get (): On the page asynchronously fetch data, this way is the form of asynchronous binding, in my previous article has confessed, here will not say it.

Spilt (): Manipulating Strings for example:
Var str = Spit ("Liu,ming,feng", ",");
This returned STR is an array of strings: {"Liu", "Ming", "Feng"}

Substr (); string manipulation, going inside the substring
Usage:
Determines whether the last character is a comma
if (str.substring (str.length-1, str.length) = = "," | | str.substring (STR.LENGTH-1, str.length) = = ",") {
Alert ("The last character is a comma!") ”);
}

HTML (): Modifies the contents of an HTML label, for example:
$ ("#div_keycontent"). HTML ("<p> no Data </p>");

Keyup (); the keyboard presses the Bouncing Trigger method
$ ("#txtSearchKey"). KeyUp (function () {
$ ("#div_keycontent"). HTML ("<p> ....</p> in Data Retrieval");
});

Trim (): Remove the trailing space of the string
Usage: Str.trim ();

Show (): Let the page element display for example: $ ("#txtSearchKey"). Shows ();
Hide (): Hides the elements of a page for example: $ ("#txtSearchKey"). Hide ();

Indexof (): see if there is a substring in the string
Usage:
if (Str.indexof (',,, ')!=-1 | | str.indexof (',,, ')!=-1) {///Determine whether there are even commas
Alert ("With two commas!") ”);
}

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.