Home >Web Front-end >JS Tutorial >Rookie JavaScript Basics 1_Basic Knowledge

Rookie JavaScript Basics 1_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 18:14:321063browse

1

Copy code The code is as follows:

//Write html content into the page
document.write("

Hello World!

")

2
Copy code The code is as follows:

//In order to prevent browsers that do not support JavaScript from displaying js as the content of the page
//The two at the end of the comment line The forward slash is a JavaScript comment symbol that prevents the JavaScript compiler from compiling this line.


//The second method is called directly using the window function. onload event


4
//JavaScript 放置的位置
当页面载入时,会执行位于 body 部分的 JavaScript。(直接执行)
当被调用时,位于 head 部分的 JavaScript 才会被执行。
head 部分
包含函数的脚本位于文档的 head 部分。这样我们就可以确保在调用函数前,脚本已经载入了。

5.
//分号的作用
//分号是可选的(根据 JavaScript 标准),浏览器把行末作为语句的结尾,通过使用分号,可以在一行中写多条语句。

6。
//JavaScript 变量名称的规则:
变量对大小写敏感(y 和 Y 是两个不同的变量)
变量必须以字母或下划线开始

7。
//变量的声明
如果您所赋值的变量还未进行过声明,该变量会自动声明。
例:
x=5; carname="Volvo";
与后面的这些语句的效果相同:var x=5; var carname="Volvo";

8。
//比较运算符
运算符      描述    例子
=== 全等(值和类型)  x===5 为 true; x==="5" 为 false

9。
//条件运算符(三目运算符)
JavaScript 还包含了基于某些条件对变量进行赋值的条件运算符。
name=("liuhuan"=="LH")?"刘欢":"歌星";


10。
//获得当前系统时间(小时数)
var d = new Date()
var time = d.getHours()

11。
//随机数
var num=Math.random();
产生的伪随机数介于 0 和 1 之间(含 0,不含 1),也就是,返回值可能为0,但总是小于1。在第一次加载 JScript 时随机数发

生器自动产生 。

12。
//获取今天的星期数(星期日为0,星期1-6为1-6)
var d = new Date()
theDay=d.getDay()

13。
//按钮的触发事件

14。
//弹出框内容换行
alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")

15.
//确认框(删除方法)
//confirm("文本")

16.
//于用户交互的弹出框(可输入文字的提示框)
//prompt("文本","默认值")

17。
//带有参数并返回值的函数





18。
//for循环 (本例中动态生成html中的h标签)


19。
//break跳出语句

解释:循环会在 i=3 时中断。


20。
//continue跳出语句

解释:当 i=3 时,会中断循环,并从下一个值开始继续循环。


值为:01245678910

21。
//for in循环(相当于.net中的foreach循环)

 
 
 

 


22.
//javascript event
onload A certain page or image is loaded //Page loading
onunload User exits the page

onfocus The element gets focus
onblur The element loses focus //Form validation
onchange The user changes the content of the field
onreset The reset button is clicked
onsubmit The submit button is clicked //Used for submission Validate all form fields before forming.
For example:
(When the user clicks the confirmation button in the form, the checkForm() function will be called. The return value of the checkForm() function is of bool type. If the return value is true, then

Submit the form, otherwise cancel the submission. )

onkeydown A certain keyboard key is pressed
onkeypress A certain keyboard key is pressed or held //Keyboard operation
onkeyup A certain keyboard key is released

onclick The mouse clicks on an object
ondblclick The mouse double-clicks on an object
onmousedown A mouse button is pressed //Mouse operation
onmousemove The mouse is moved
onmouseout The mouse moves away from an element
onmouseover The mouse is moved over an element
onmouseup A mouse button is released

onabort Image loading interrupted
onerror An error occurred while loading the document or image

onresize The window or frame is resized
onselect The text is selected

23.
//Error message err.description and its try...catch statement in js
For example:

24.
//try...catch statement with confirmation box


26.
//Usage of return true and return true
(It can return a bool parameter, which can be used for judgment)
function jiance(msg,url,l){
alert("You Are you sure?")
return true
}
function jieguo(){
if(jiance()){
alert("Yes");
}
else {
alert("No");
}
}

27.
//onerror event





28.
//Backslashes are used in JavaScript to add special characters to text strings.
For example:
var txt="We are the so-called "Vikings" from the north."
document.write(txt)

29.
//Javascript Notes
1. JavaScript is case sensitive
2. JavaScript will ignore extra whitespace
3. You can use backslashes for line breaks when writing code
Example:
document.write("Hello

World!");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn