Home Web Front-end JS Tutorial JavaScript Basic Questions and Answers 4_Basic Knowledge

JavaScript Basic Questions and Answers 4_Basic Knowledge

May 16, 2016 pm 06:58 PM
javascript

 二、导航功能增强

 

1. 下拉菜单中的链接(Links in Select Menu

Q:我如何实现在下拉菜单中链接到不同的页面?

A:要创建一个所示的下拉菜单:

你可以使用下面的代码:

  1. <form>
  2. <select
  3. onChange="if(this.selectedIndex!=0)
  4. self.location=this.options[this.selectedIndex].value">
  5. <option value="" selected>Select a page
  6. <option value="startpag.htm">JavaScript FAQ
  7. <option value="numbers.htm">Numbers
  8. <option value="strings.htm">Strings
  9. <option value="navigati.htm">Navigation
  10. <option value="colors.htm">Colors
  11. <option value="http://www.javascripter.net">JavaScripter.net
  12. select>
  13. form> 

只需要把菜单项及其相应的URL改为你需要就可以了。你可以使用绝对地址(就像http://www.javascripter.net),也可以使用相对地址(像 mypage.htm)。

 

2.  按钮链接(Button Links

Q:我怎么才能把一个按钮变为指向另外一个页面的超链接呢?

A:要创建一个按钮就像一个:

你可以使用这段代码:

  1. <form> 
  2. <input type=button
  3. value="insert button text here"
  4. onClick="self.location='Your_URL_here.htm'">
  5. form> 

只需要改为你需要的按钮文本和目标地址。试一下这个:

你可以使用绝对地址(像http://www.javascripter.net)也可以使用相对地址(像mypage.htm)。

 

3.  后退按钮(Back Button

Q:我能让按钮像浏览器的“后退”按钮一样吗?

A:要创建你自己的后退按钮,可以使用这段代码:

  1. <form>
  2. <input type=button value="Back"
  3. onCLick="history.back()">
  4. form>

现在试一下:

 

4. 前进按钮(Forward Button

Q:我能让按钮像浏览器中的“前进”按钮一样吗?

A:要创建自己的“前进”按钮,使用这段代码:

  1. <form>
  2. <input type=button value="Forward"
  3. onCLick="history.forward()">
  4. form>

如果浏览器上的前进按钮当前不可用,那么这个“前进”按钮同样不能工作。这种情况就是当前页是你浏览历史中的最后一页。换句话说,如果你是使用浏览器的“后退”按钮到达的这个页面(或者脚本编写的后退按钮),那么这个前进按钮就可以工作。现在试一下吧!

 

5. 查询字符串(Query Stirngs

Q:我的脚步可以访问当前URL中的查询字符串吗?

A:查询字符串(或搜索字符串)是URL中的一个可选部分,它跟在文件名后面,以问号引导(?)。例如,下面的URL在HTML文件名后包含了一个查询字符串 ?myquery

http://www.myfirm.com/file.html?myquery

你的脚本可以使用JavaScript的location.search属性访问当前URL中的查询字符按。点击下面按钮试一下看看!(为了查看地址中的URL,你可能想要在顶层浏览器窗口中显示这个页面。)

创建这些按钮的代码是:

  1. <form>
  2. <input type=button value="Add query ?test"
  3. onClick="selfself.location=
  4. self.location.protocol+'//'
  5. +self.location.host
  6. +self.location.pathname+'?test'">
  7. <input type=button value="Show query"
  8. onClick="alert('Query string: '+self.location.search)">
  9. <input type=button value="Remove query"
  10. onClick="selfself.location=
  11. self.location.protocol+'//'
  12. +self.location.host
  13. +self.location.pathname">
  14. form>

注意:查询字符串有时候可能不会如预期一样的工作。例如,如果你将这个页面保存本地磁盘上,上面在Internet Explorer 4.x就不会工作(但是在Netscape Navigator中依然有效)。

 

6. 向页面传递参数(Passing parameters to a page

Q:我可以从也页面向另外一个页面传递参数吗?

A:可以。有几种不同的方式可以实现:

  • 把参数保存在cookie
  • 把参数保存在另外一个窗口或框架的变量中
  • 把参数存在可以修改的属性top.name(浏览器窗口的名字)中
  • 把参数作为一个查询字符串拼接在目标页面的URL后面

这里是一个简单的例子来演示所有这些传递参数的方法。传递的值应该是字符换“It_worked”。当你点击下面的按钮时,按钮的事件脚本会存在这些值(1)在名为parm_value的cookie中,(2)以顶层变量top.parm_value保存以及(3)在top.name属性中。然后,脚本引导浏览器到parm_get.htm,它的URL包含一个值为URL编码的查询字符串。

 

7. 查找文本(Searching for text

Q:我怎样在页面查询一个特定的文本字符串?

A:在Netscape Navigator 4.x中,可以使用window.find(string) 方法查找;参见查找对话框。在Internet Explorer 4.x或更新版本中,创建一个文本范围对象(下面的例子中是TRang),然后使用TRang.findText(string)。

示例:下面的脚本根据用户输入的文本查找并在页面上高亮显示。

这个示例的代码为:

  1. <form name="f1" action="" 
  2. onSubmit="if(this.t1.value!=null && this.t1.value!='')
  3. findString(this.t1.value);return false"
  4. >
  5. <input type="text" name=t1 value="" size=20>
  6. <input type="submit" name=b1 value="Find">
  7. form>
  8. <script language="JavaScript">
  9. 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)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    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 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.

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

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).

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles