JavaScript Basic Questions and Answers 4_Basic Knowledge
二、导航功能增强
1. 下拉菜单中的链接(Links in Select Menu)
Q:我如何实现在下拉菜单中链接到不同的页面?
A:要创建一个所示的下拉菜单:
你可以使用下面的代码:
- <form>
- <select
- onChange="if(this.selectedIndex!=0)
- self.location=this.options[this.selectedIndex].value">
- <option value="" selected>Select a page
- <option value="startpag.htm">JavaScript FAQ
- <option value="numbers.htm">Numbers
- <option value="strings.htm">Strings
- <option value="navigati.htm">Navigation
- <option value="colors.htm">Colors
- <option value="http://www.javascripter.net">JavaScripter.net
- select>
- form>
只需要把菜单项及其相应的URL改为你需要就可以了。你可以使用绝对地址(就像http://www.javascripter.net),也可以使用相对地址(像 mypage.htm)。
2. 按钮链接(Button Links)
Q:我怎么才能把一个按钮变为指向另外一个页面的超链接呢?
A:要创建一个按钮就像一个:
你可以使用这段代码:
- <form>
- <input type=button
- value="insert button text here"
- onClick="self.location='Your_URL_here.htm'">
- form>
只需要改为你需要的按钮文本和目标地址。试一下这个:
你可以使用绝对地址(像http://www.javascripter.net)也可以使用相对地址(像mypage.htm)。
3. 后退按钮(Back Button)
Q:我能让按钮像浏览器的“后退”按钮一样吗?
A:要创建你自己的后退按钮,可以使用这段代码:
- <form>
- <input type=button value="Back"
- onCLick="history.back()">
- form>
现在试一下:
4. 前进按钮(Forward Button)
Q:我能让按钮像浏览器中的“前进”按钮一样吗?
A:要创建自己的“前进”按钮,使用这段代码:
- <form>
- <input type=button value="Forward"
- onCLick="history.forward()">
- form>
如果浏览器上的前进按钮当前不可用,那么这个“前进”按钮同样不能工作。这种情况就是当前页是你浏览历史中的最后一页。换句话说,如果你是使用浏览器的“后退”按钮到达的这个页面(或者脚本编写的后退按钮),那么这个前进按钮就可以工作。现在试一下吧!
5. 查询字符串(Query Stirngs)
Q:我的脚步可以访问当前URL中的查询字符串吗?
A:查询字符串(或搜索字符串)是URL中的一个可选部分,它跟在文件名后面,以问号引导(?)。例如,下面的URL在HTML文件名后包含了一个查询字符串 ?myquery:
http://www.myfirm.com/file.html?myquery
你的脚本可以使用JavaScript的location.search属性访问当前URL中的查询字符按。点击下面按钮试一下看看!(为了查看地址中的URL,你可能想要在顶层浏览器窗口中显示这个页面。)
创建这些按钮的代码是:
- <form>
- <input type=button value="Add query ?test"
- onClick="selfself.location=
- self.location.protocol+'//'
- +self.location.host
- +self.location.pathname+'?test'">
- <input type=button value="Show query"
- onClick="alert('Query string: '+self.location.search)">
- <input type=button value="Remove query"
- onClick="selfself.location=
- self.location.protocol+'//'
- +self.location.host
- +self.location.pathname">
- form>
注意:查询字符串有时候可能不会如预期一样的工作。例如,如果你将这个页面保存本地磁盘上,上面在Internet Explorer 4.x就不会工作(但是在Netscape Navigator中依然有效)。
6. 向页面传递参数(Passing parameters to a page)
Q:我可以从也页面向另外一个页面传递参数吗?
A:可以。有几种不同的方式可以实现:
这里是一个简单的例子来演示所有这些传递参数的方法。传递的值应该是字符换“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)。
示例:下面的脚本根据用户输入的文本查找并在页面上高亮显示。
这个示例的代码为:
- <form name="f1" action=""
- onSubmit="if(this.t1.value!=null && this.t1.value!='')
- findString(this.t1.value);return false"
- >
- <input type="text" name=t1 value="" size=20>
- <input type="submit" name=b1 value="Find">
- form>
- <script language="JavaScript">
-
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
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 Settings3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌Assassin's Creed Shadows: Seashell Riddle Solution1 weeks ago By DDDR.E.P.O. How to Fix Audio if You Can't Hear Anyone3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌Where to find the Crane Control Keycard in Atomfall1 weeks ago By DDDHot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
CakePHP Tutorial1359
52

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

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

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

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

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
