Js usage Summary

Source: Internet
Author: User

Js usage Summary

1. Periodically execute functions

The setTimeout () method is used to call a function or computing expression after a specified number of milliseconds.
Example:

<Script type = "text/javascript"> var c = 0; var t; function timedCount () {document. getElementById ('txt '). value = c; c = c + 1; t = setTimeout ("timedCount ()", 1000) ;}</script> or:<Script type = "text/javascript"> function timedCount (c) {document. getElementById ('txt '). value = c; c = c + 1; var x = function () {timedCount (c);} setTimeout (x, 1000) ;}</script>
2. How to determine whether an object exists in js

Typeof function:
Typeof can be used to detect the Data Type of a given variable. Possible return values:
1. 'undefined' --- the value is undefined;
2. 'boolean' --- the value is a boolean value;
3. 'string' --- the value is a string;
4. 'number' --- the value is a numerical value;
5. 'object' --- the value is an object or null;
6. 'function' --- the value is a function.
Example:
If (typeof (key )! = "Undefined" & key! = '') {// Do something}

3. Replace spaces in js

Use a. replace (/\ s +/g, ''); example:

<script type="text/javascript">var a = ' 11 222 33 44 55 ';a = a.replace(/\s+/g,'');alert(a);</script>
4. string segmentation Functions

Split (): Splits a string into a string array.
StringObject. split (separator, howator)
Example:

<script type="text/javascript">var data = "1,2,3,4";var arr = data.split(",");alert(arr[0]);</script>
5. Delete elements from the array

Splice (): deletes elements from an array.
ArrayObject. splice (index, howmany, item1,..., itemX)
Index: required. Integer that specifies the position of the added/deleted project. A negative number can be specified from the end of the array.
Howmany: required. The number of projects to delete. If it is set to 0, the project will not be deleted.
Item1,..., itemX: Optional. Add a new project to the array.
Note:
The splice () method deletes zero or multiple elements starting from the index and replaces the deleted elements with one or more values declared in the parameter list. If an element is deleted from an arrayObject, an array containing the deleted element is returned.

6. Convert JSON to a string
Str = JSON. stringify (data); // data is json-type data

7. Search for the number of substrings
// Search for the number of song_id in data
Var reg = new RegExp ('song _ id', "gi ");
Count = str. match (reg). length;

8. Changes in the Listener Input box

function immediately(){var element = document.getElementById("title");if("\v"=="v") {element.onpropertychange = webChange;}else{element.addEventListener("input",webChange,false);}function webChange(){if(element.value){//do something with element.value};}}immediately();
Reference: http://www.jb51.net/article/27684.htm

9. Delete All subnodes under the div

Function removeAllChild () {var div = document. getElementById ("songLink"); while (div. hasChildNodes () {// when a child node still exists under the div, The div continues cyclically. removeChild (div. firstChild );}}
10. Jquery modifies css attributes
Css attributes can be dynamically modified using the css () method in jquery. The following describes common methods:
1. Get css attributes: ((selector).css (name)
Get the value of the color Style attribute of the First Section: $ ("p" ).css ("color ");
2. Set css attributes: $(selector).css (name, value)
Set the color of all paragraphs to red: $ ("p" ).css ("color", "red ");
3. Use a function to set CSS attributes: vertex (selector).css (name, function (index, value ))
This function returns the attribute value to be set. Take two parameters. index indicates the index position of the element in the object set (Optional). value indicates the original attribute value (optional ).
Set the color of all paragraphs to Red:
$ ("Button"). click (function (){
$ ("P" ).css ("color", function () {return "red ";});
});
4. Set multiple CSS attributes/value pairs: $(selector).css ({property: value, property: value ,...})
$ ("P" ).css ({
"Color": "white ",
"Background-color": "#98bf21 ",
"Font-family": "Arial ",
"Font-size": "20px ",
"Padding": "5px"
});
Note: Jquery can use the attr () function to set the attribute value. The usage is the same as that of css ().
For more information, see Jquery attribute operations.


This article is original Eliot, reprinted please indicate the source: http://blog.csdn.net/xyw_blog/article/details/40432313

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.