Home Web Front-end JS Tutorial Summary of JavaScript document methods

Summary of JavaScript document methods

Nov 28, 2016 pm 03:10 PM
javascript

The following methods are very commonly used, record them to free up some brain space~

1

2

3

4

5

6

7

8

9

10

11

12

document.title      //设置文档标题等价于HTML的<title>标签

document.bgColor    //设置页面背景色

document.fgColor    //设置前景色(文本颜色)

document.linkColor  //未点击过的链接颜色

document.alinkColor //激活链接(焦点在此链接上)的颜色

document.vlinkColor //已点击过的链接颜色

document.URL        //设置URL属性从而在同一窗口打开另一网页

document.fileCreatedDate    //文件建立日期,只读属性

document.fileModifiedDate   //文件修改日期,只读属性

document.fileSize   //文件大小,只读属性

document.cookie     //设置和读出cookie

document.charset    //设置字符集 简体中文:gb2312

Copy after login

Commonly used object methods:

1

2

3

4

5

document.write()                //动态向页面写入内容

document.createElement(Tag)     //创建一个html标签对象

document.getElementById(ID)     //获得指定ID值的对象

document.getElementsByName(Name)    //获得指定Name值的对象

document.body.appendChild(oTag)

Copy after login

body Main sub-object:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

document.body           //指定文档主体的开始和结束等价于<body></body>

document.body.bgColor   //设置或获取对象后面的背景颜色

document.body.link      //未点击过的链接颜色

document.body.alink     //激活链接(焦点在此链接上)的颜色

document.body.vlink     //已点击过的链接颜色

document.body.text      //文本色

document.body.innerText     //设置<body>...</body>之间的文本

document.body.innerHTML     //设置<body>...</body>之间的HTML代码

document.body.topMargin     //页面上边距

document.body.leftMargin    //页面左边距

document.body.rightMargin   //页面右边距

document.body.bottomMargin  //页面下边距

document.body.background    //背景图片

document.body.appendChild(oTag)     //动态生成一个HTML对象

Copy after login

Commonly used object events:

document.body.onclick="func()" //Clicking the object with the mouse pointer is triggered

document.body.onmouseover="func() " //Triggered when the mouse pointer moves to the object

document.body.onmouseout="func( ;

1

2

3

4

5

6

7

8

document.location.hash      // #号后的部分

document.location.host      // 域名+端口号

document.location.hostname  // 域名

document.location.href      // 完整URL

document.location.pathname  // 目录部分

document.location.port      // 端口号

document.location.protocol  // 网络协议(http:)

document.location.search    // ?号后的部分

Copy after login

Directly reference through the nane attribute

1

2

3

4

documeny.location.reload()      //刷新网页

document.location.reload(URL)   //打开新的网页

document.location.assign(URL)   //打开新的网页

document.location.replace(URL)  //打开新的网页

Copy after login

Reference the src attribute of the image

1

document.selection

Copy after login

Create an image

1

2

3

4

document.images //对应页面上的<img>标签

document.images.length //对应页面上<img>标签的个数

document.images[0] //第1个<img>标签

document.images[i] //第i-1个<img>标签

Copy after login

At the same time, create an tag on the page corresponding to it to display

Sample code (dynamically created Image):

1

2

<img>

document.images.oImage //document.images.name属性

Copy after login

forms collection (form in the page):

Referenced through the collection

1

document.images.oImage.src //document.images.name属性.src

Copy after login

Directly referenced through the tag name attribute

1

2

3

var oImage

oImage = new Image()

document.images.oImage.src="1.jpg"

Copy after login

Accessing the properties of the form:

1

2

3

4

5

6

7

8

<html>

<img>

<script>

    var oImage

    oImage = new Image()

    document.images.oImage.src="1.jpg"

</script>

</html>

Copy after login

Sample code (form):

1

2

3

4

5

6

document.forms          //对应页面上的<form>标签

document.forms.length   //对应页面上<form>标签的个数

document.forms[0]       //第1个<form>标签

document.forms[i]       //第i-1个<form>标签

document.forms[i].length        //第i-1个<form>中的控件数

document.forms[i].elements[j]   //第i-1个<form>中第j-1个控件

Copy after login

Sample code (checkbox):

1

2

<form><input></form>

document.Myform.myctrl //document.表单名.控件名

Copy after login

Sample code (Select):

1

2

3

4

5

document.forms[i].name      //对应<form name>属性

document.forms[i].action    //对应<form action>属性

document.forms[i].encoding  //对应<form enctype>属性

document.forms[i].target    //对应<form target>属性

document.forms[i].appendChild(oTag)     //动态插入一个控件

Copy after login

Div collection (layer in the page):

1

2

3

4

5

6

7

8

9

10

11

12

<html>

<!--Text控件相关Script-->

<form>

<input>

<input>

<form>

<script>

//获取文本密码框的值

document.write(document.Myform.oText.value)

document.write(document.Myform.oPswd.value)

</script>

</html>

Copy after login

4 properties of the layer object:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<html>

<!--checkbox,radio控件相关script-->

<form>

<input value="1">1

<input value="2">2

</form>

<script>

function fun()

{

 //遍历checkbox控件的值并判断是否选中

    var length

    length=document.forms[0].chk.length

    for(i=0;i<length;i++){

    v=document.forms[0].chk[i].value

    b=document.forms[0].chk[i].checked

    if(b)

    alert(v=v+"被选中")

        else

    alert(v=v+"未选中")

    }

}

</script>

<a href="http://www.php1.cn/">ddd</a>

</html>

Copy after login

Sample code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<html>

<!--Select控件相关Script-->

<form>

<select>

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

</select>

</form>

<script>

 //遍历select控件的option项

    var length

    length=document.Myform.oSelect.length

    for(i=0;i<length;i++)

        document.write(document.Myform.oSelect[i].value)

</script>

<script>

    //遍历option项并且判断某个option是否被选中

    for(i=0;i<document.Myform.oSelect.length;i++)

    {

        if(document.Myform.oSelect[i].selected!=true)

            document.write(document.Myform.oSelect[i].value)

        else

            document.write("<font color=red>"+document.Myform.oSelect[i].value+"</font>")

    }

</script>

<script>

    //根据SelectedIndex打印出选中的option

    //(0到document.Myform.oSelect.length-1)

    i=document.Myform.oSelect.selectedIndex

    document.write(document.Myform.oSelect[i].value)

</script>

<script>

    //动态增加select控件的option项

    var oOption = document.createElement("OPTION");

    oOption.text="4";

    oOption.value="4";

    document.Myform.oSelect.add(oOption);

</script>

<html>

Copy after login

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles