search
HomeWeb Front-endJS TutorialRookie JavaScript Basics 1_Basic Knowledge

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.

3
Copy code The code is as follows:

//Multiple ways to write onload events
//The first way is to add the onload event through the body tag


//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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools