A collection of 100 common tricks for javascript

1. JS output statement: document.write("")

2. The comment in JS is ://

3. The traditional HTML document order is: document->html->(head,body)

4. In a browser window The DOM order is: window->(navigator, screen, history, location, document)

5. Get the name and value of the element in the form: document.getElementById("ID number of the element in the form").name (or value)

6 .A lowercase to uppercase JS:

document.getElementById("output").value = document.getElementById("input").value.toUpperCase();
7. Value types in JS: String, Number, Boolean, Null, Object, Function

8. Convert the character type in JS to numeric type: parseInt(), parseFloat() 9. Convert the number in JS to character type

: ("" + variable)

10. The length of the string in JS is: (length)

11. The characters in JS are connected with characters using the + sign.

12. The comparison operators in JS are: == equal, != not equal, >, >=, <.<=

13. Declaration in JS Variables use: var to declare

14. Judgment statement structure in JS: if(condition){}else{}

15. The loop structure in JS: for([initial e¬xpression];[condition];[upadte e¬xpression]) {inside loop}

16. The command to terminate the loop is: break

17. Function definition in JS: function functionName([parameter],…){statement[s]}

18. When there are multiple form forms in the file. You can use document.forms[0], document.forms[1] instead.

19. Window: open the window window.open(), close a window: window.close(), the window itself: self

20. Status bar settings: window.status=”character”;

21. Pop-up prompt message: window.alert(“character”);

22. Pop-up confirmation box: window.confirm();

23. Pop-up input prompt box: window.prompt();

24. Specify the location of the currently displayed link: window.location.href=”/blog/URL”

25. Take out the window The number of all forms in the body: document.forms.length

26. Close the document's output stream: document.close();

27. String appender: +=

28. Create a document element: document.createElement(), document.createTextNode()

29. The method to get the element: document.getElementById()

30. Set the value of all text-type members in the form to be empty:

var form = window.document.forms[0]

for (var i = 0; i

if (form.elements.type == “text”){

form. elements.value = "";

}

}
31. Check whether the check button is selected in JS: document.forms[0].checkThis.checked (checked attribute represents whether it is selected and returns TRUE or FALSE)

32. Radio button group ( The names of the radio buttons must be the same): take the length of the radio button group document.forms[0].groupName.length

33. The radio button group is also checked to determine whether it is selected.

34. The value of the drop-down list box: document. forms[0].selectName.options[n].value (n sometimes uses the drop-down list box name plus .selectedIndex to determine the selected value)

35. String definition: var myString = new String("This is lightsword" );

36. Convert the string to uppercase: string.toUpperCase(); Convert the string to lowercase: string.toLowerCase();

37. Return the position where string 2 appears in string 1: String1.indexOf(“String2” )!=-1 means not found.

38. Take a character at the specified position in the string: StringA.charAt(9);

39.取出字符串中指定起点和终点的子字符串:stringA.substring(2,6);

40.数学函数:Math.PI(返回圆周率),Math.SQRT2(返回开方),

Math.max(value1,value2)返回两个数中的最在值,

Math.pow(value1,10)返回value1的十次方,

Math.round(value1)四舍五入函数,

Math.floor(Math.random()*(n+1))返回 随机数

41.定义日期型变量:var today = new Date();

42.日期函数列表:

dateObj.getTime()得到时间,

dateObj.getYear()得到年份,

dateObj.getFullYear()得到四位的年份,

dateObj.getMonth()得到月份,

dateObj.getDate()得到日,

dateObj.getDay()得到日期几,

dateObj.getHours()得到小时,

dateObj.getMinutes()得到分,

dateObj.getSeconds()得到秒,

dateObj.setTime(value)设置时间,

dateObj.setYear(val)设置年,

dateObj.setMonth(val)设置月,

dateObj.setDate(val) sets the day,

dateObj.setDay(val) sets the day of the week,

dateObj.setHours sets the hour,

dateObj.setMinutes(val) sets the minute,

dateObj.setSeconds(val) sets the second [Note: This date and time starts from 0 Start counting]

43.FRAME representation:

[window.]frames[n].ObjFuncVarName, frames[“frameName”].ObjFuncVarName, frameName.ObjFuncVarName

44.parent represents the parent object, top represents the top object

45. Open the child window The parent window is: opener

46. Indicates the current location: this

47. When calling a JS function in a hyperlink, use: (javascript: ) to start with the function name

48. This JS is not executed in old browsers :

49. Quote a file-style JS:

50. Specify HTML to be displayed in browsers that do not support scripting:

51. When both hyperlinks and onCLICK events are present, older browsers turn to a.html, otherwise turn to b. html. Example: dfsadf

52. The built-in objects of JS are:

Array,Boolean,Date,Error,EvalError,Function,Math,Number,Object,RangeError,ReferenceError,RegExp,String,SyntaxError,TypeError,URIError

53.Newline in JS:n

54.isNaN(variable): test whether it is a value Type

55.all in JS represents all the elements below

it , then the value of innerHTML is: how are you 58. The value of innerTEXT is the same as the above, except that this kind of mark will not be displayed. 59. contentEditable can set whether the element can be modified, isContentEditable returns the status of whether it can be modified . 60.isDisabled determines whether it is in the forbidden state. Disabled sets the forbidden state. 61.length gets the length and returns an integer value . 62.addBehavior() is an external function file called by JS. Its extension is .htc 63.window.focus( ) makes the current window in front of all windows. 64.blur() means losing focus. Contrary to FOCUS(). 65.select() means that the element is selected.























66. Prevent the user from entering text into the text box: onfocus=”this.blur()”

67. Take out the number of elements that appear in the page: document.all.tags(“div (or other HTML tags)”). length

68. There are two forms of output in JS: modal and non-modal. window.showModaldialog(), window.showModeless()

69. Status bar text settings: window.status='text', the default state Column text settings: window.defaultStatus = 'Text.';

70. Add to Favorites: external.AddFavorite("Http://www.kubiji.cn/";,"jaskdlf");

71. Script encountered in JS Do nothing when there is an error: window.onerror = doNothing; The syntax for specifying the error handle is: window.onerror =

handleError; 72. JS specifies the parent window of the currently opened window: window.opener, which supports multiple continuations of opener.opener… .

73. self in JS refers to the current window

74. JS shows the content of the status bar: window.status=”content”

75. top in JS refers to the topmost frame

in the frame set 76. JS closes the current Window: window.close();

77. The box for confirmation in JS: if(confirm("Are you sure?")){alert("ok");}else{alert("Not Ok"); }

78. Window redirection in JS: window.navigate("http://www.kubiji.cn/";);

79. Printing in JS: window.print()

80. Prompt input box in JS: window .prompt("message", "defaultReply");

81. Window scroll : window.scroll(x, y)

82. Window scrolling to position in JS: window.scrollby

83. Set time interval in JS: setInterval("expr", msecDelay) or setInterval(funcRef, msecDelay) or setTimeout

84. The modal in JS is shown in IE4+ line, not in NN: showModalDialog("URL"[,arguments][,features]);

85 The handle used before exit in .JS: function verifyClose(){event.returnValue=”we really like you and hope you will stay longer.”;}} window.onbeforeunload=verifyClose;

86. When the form is called for the first time The file handle used when: onload()

87. The file handle called when the form is closed: onunload()

88. The property of window.location:

protocol(http : ),hostname(www.kubiji.cn),port(80),host(www.kubiji.cn:80),pathname(“/a/a.html”),hash(“#giantGizmo”, Refers to jumping to the corresponding anchor), href (all information)

89.window.location.reload() refreshes the current page.

90.window.history.back() returns to the previous page, window.history.forward() Return to the next page, window.history.go (return the number of pages, you can also use the visited URL)

91.document.write() output without wrapping, document.writeln() wrapping output

92.document.body.noWrap =true; Prevent link text from wrapping.

93. Variable name. charAt (number of digits), take the number of characters of the variable.

94. "abc".charCodeAt (number of digits), return the number of characters ASCii code value.

95. String concatenation: string.concat(string2), or use += for

concatenation )

97.string.lastIndexOf(searchString[,startIndex]) The position of the last occurrence.

98.string.match(regExpression), judge whether the characters match.

99.string.replace(regExpression,replaceString) replace the existing string.

100.string.split (delimiter) returns an array to store the value.

Original link: http://www.kubiji.cn/topic-id3161.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989910&siteId=291194637