---
title: "js 实例 some2 "
author: "bobshute"
date: "2009-04-07T21:29:09Z"
tags: ["C#", "HTML", "JavaScript", "Opera", "浏览器"]
slug: "js-some2"
draft: false
lastmod: "2009-04-07T21:29:09Z"
id: 363031
url: "https://bobshute.iteye.com/blog/363031"
---

1.&lt;html&gt;  
&lt;body&gt;  
&lt;script type="text/javascript"&gt;  
var browser=navigator.appName  
var b\_version=navigator.appVersion  
var version=parseFloat(b\_version)  
document.write("浏览器名称："+ browser)  
document.write("  
")  
document.write("浏览器版本："+ version)  
&lt;script type="text/javascript"&gt;  
document.write("&lt;p&gt;浏览器：")  
document.write(navigator.appName + "&lt;/p&gt;")  
document.write("&lt;p&gt;浏览器版本：")  
document.write(navigator.appVersion + "&lt;/p&gt;")  
document.write("&lt;p&gt;代码：")  
document.write(navigator.appCodeName + "&lt;/p&gt;")  
document.write("&lt;p&gt;平台：")  
document.write(navigator.platform + "&lt;/p&gt;")  
document.write("&lt;p&gt;Cookies 启用：")  
document.write(navigator.cookieEnabled + "&lt;/p&gt;")  
document.write("&lt;p&gt;浏览器的用户代理报头：")  
document.write(navigator.userAgent + "&lt;/p&gt;")  
var x = navigator;  
document.write("CodeName=" + x.appCodeName);  
document.write("  
");  
document.write("MinorVersion=" + x.appMinorVersion);  
document.write("  
");  
document.write("Name=" + x.appName);  
document.write("  
");  
document.write("Version=" + x.appVersion);  
document.write("  
");  
document.write("CookieEnabled=" + x.cookieEnabled);  
document.write("  
");  
document.write("CPUClass=" + x.cpuClass);  
document.write("  
");  
document.write("OnLine=" + x.onLine);  
document.write("  
");  
document.write("Platform=" + x.platform);  
document.write("  
");  
document.write("UA=" + x.userAgent);  
document.write("  
");  
document.write("BrowserLanguage=" + x.browserLanguage);  
document.write("  
");  
document.write("SystemLanguage=" + x.systemLanguage);  
document.write("  
");  
document.write("UserLanguage=" + x.userLanguage);  
&lt;/script&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
2\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function detectBrowser()  
{  
var browser=navigator.appName  
var b\_version=navigator.appVersion  
var version=parseFloat(b\_version)  
if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version&gt;=4))  
  {alert("您的浏览器够先进了！")}  
else  
  {alert("是时候升级您的浏览器了！")}  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onload="detectBrowser()"&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
3\.  
创建cookie：  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function getCookie(c\_name)  
{  
if (document.cookie.length&gt;0)  
{  
c\_start=document.cookie.indexOf(c\_name + "=")  
if (c\_start!=-1)  
{  
c\_start=c\_start + c\_name.length+1  
c\_end=document.cookie.indexOf(";",c\_start)  
if (c\_end==-1) c\_end=document.cookie.length  
return unescape(document.cookie.substring(c\_start,c\_end))  
}  
}  
return ""  
}  
function setCookie(c\_name,value,expiredays)  
{  
var exdate=new Date()  
exdate.setDate(exdate.getDate()+expiredays)  
document.cookie=c\_name+ "=" +escape(value)+  
((expiredays==null) ? "" : "; expires="+exdate.toGMTString())  
}  
function checkCookie()  
{  
username=getCookie('username')  
if (username!=null && username!="")  
  {alert('Welcome again '+username+'!')}  
else  
  {  
  username=prompt('Please enter your name:',"")  
  if (username!=null && username!="")  
    {  
    setCookie('username',username,365)  
    }  
  }  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onLoad="checkCookie()"&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
4\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function whichButton(event)  
{  
if (event.button==2)  
{  
alert("您点击了鼠标右键！")  
}  
else  
{  
alert("您点击了鼠标左键！")  
}  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="whichButton(event)"&gt;  
&lt;p&gt;请在文档中点击。一个消息框会提示出你点击了哪个鼠标按键。&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
5\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function whichButton(event)  
{  
alert(event.keyCode)  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onkeyup="whichButton(event)"&gt;  
&lt;p&gt;&lt;b&gt;注释：&lt;/b&gt;在测试这个例子时，要确保右侧的框架获得了焦点。&lt;/p&gt;  
&lt;p&gt;在键盘上按一个键。消息框会提示出该按键的 unicode。&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
6.&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function isKeyPressed(event)  
{  
  if (event.shiftKey==1)  
    {  
    alert("The shift key was pressed!")  
    }  
  else  
    {  
    alert("The shift key was NOT pressed!")  
    }  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="isKeyPressed(event)"&gt;  
&lt;p&gt;在文档中点击某个位置。消息框会告诉你是否按下了 shift 键。&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
7  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function whichElement(e)  
{  
var targ  
if (!e) var e = window.event  
if (e.target) targ = e.target  
else if (e.srcElement) targ = e.srcElement  
if (targ.nodeType == 3) // defeat Safari bug  
   targ = targ.parentNode  
var tname  
tname=targ.tagName  
alert("You clicked on a " + tname + " element.")  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="whichElement(event)"&gt;  
&lt;p&gt;在文档中点击某个位置。消息框会提示出您所点击的标签的名称。&lt;/p&gt;  
&lt;h3&gt;这是标题&lt;/h3&gt;  
&lt;p&gt;这是段落。&lt;/p&gt;  
&lt;img src="/i/eg\_mouse2.jpg" /&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
8\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function whichElement(e)  
{  
var targ  
if (!e) var e = window.event  
if (e.target) targ = e.target  
else if (e.srcElement) targ = e.srcElement  
if (targ.nodeType == 3) // defeat Safari bug  
   targ = targ.parentNode  
var tname  
tname=targ.tagName  
alert("You clicked on a " + tname + " element.")  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="whichElement(event)"&gt;  
&lt;p&gt;在文档中点击某个位置。消息框会提示出您所点击的标签的名称。&lt;/p&gt;  
&lt;h3&gt;这是标题&lt;/h3&gt;  
&lt;p&gt;这是段落。&lt;/p&gt;  
&lt;img src="/i/eg\_mouse2.jpg" /&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
9\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function getEventType(event)  
  {  
  alert(event.type);  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="getEventType(event)"&gt;  
&lt;p&gt;在文档中点击某个位置。消息框会提示出被触发的事件的类型。&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
10  
&lt;form&gt;  
Select text: &lt;input type="text" value="Hello world!"  
onselect="alert('You have selected some of the text.')"&gt;  
&lt;br /&gt;&lt;br /&gt;  
Select text: &lt;textarea cols="20" rows="5"  
onselect="alert('You have selected some of the text.')"&gt;  
Hello world!  
11\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function setStyle(x)  
{  
document.getElementById(x).style.background="yellow"  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
First name: &lt;input type="text"  
onfocus="setStyle(this.id)" id="fname" /&gt;  
&lt;br /&gt;  
Last name: &lt;input type="text"  
onfocus="setStyle(this.id)" id="lname" /&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
12.不让输入数字的文本框。  
&lt;html&gt;  
&lt;body&gt;  
&lt;script type="text/javascript"&gt;  
function noNumbers(e)  
{  
var keynum  
var keychar  
var numcheck  
if(window.event) // IE  
{  
keynum = e.keyCode  
}  
else if(e.which) // Netscape/Firefox/Opera  
{  
keynum = e.which  
}  
keychar = String.fromCharCode(keynum)  
numcheck = /\\d/  
return !numcheck.test(keychar)  
}  
&lt;/script&gt;  
&lt;form&gt;  
Type some text (numbers not allowed):  
&lt;input type="text" onKeyDown="return noNumbers(event)" /&gt;  
&lt;/form&gt;  
&lt;/html&gt;  
13\.  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function isKeyPressed(event)  
{  
  if (event.altKey==1)  
    {  
    alert("The ALT key was pressed!")  
    }  
  else  
    {  
    alert("The ALT key was NOT pressed!")  
    }  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="isKeyPressed(event)"&gt;  
&lt;p&gt;Click somewhere in the document. An alert box will tell you if you pressed the ALT key or not.&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
14  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function isKeyPressed(event)  
{  
  if (event.ctrlKey==1)  
    {  
    alert("The CTRL key was pressed!")  
    }  
  else  
    {  
    alert("The CTRL key was NOT pressed!")  
    }  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body onmousedown="isKeyPressed(event)"&gt;  
&lt;p&gt;Click somewhere in the document. An alert box will tell you if you pressed the CTRL key or not.&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
15  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function changeLink()  
{  
document.getElementById('myAnchor').innerHTML="访问 W3School"  
document.getElementById('myAnchor').href="[http://www.w3school.com.cn](http://www.w3school.com.cn/)"  
document.getElementById('myAnchor').target="\_blank"  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;a id="myAnchor" href="[http://www.microsoft.com](http://www.microsoft.com/)"&gt;访问 Microsoft&lt;/a&gt;  
&lt;input type="button" onClick="changeLink()" value="改变链接"&gt;  
&lt;p&gt;在本例中，我们改变超链接的文本和 URL。我们也改变 target 属性。target 属性的默认设置是 "\_self"，这意味着会在相同的窗口中打开链接。通过把 target 属性设置为 "\_blank"，链接将在新窗口中打开。&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
16 Checkbox - 把文本转换为大写  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function convertToUcase()  
{  
document.getElementById("fname").value=document.getElementById("fname").value.toUpperCase()  
document.getElementById("lname").value=document.getElementById("lname").value.toUpperCase()  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form name="form1"&gt;  
名：&lt;input type="text" id="fname" size="20" /&gt;  
&lt;br /&gt;&lt;br /&gt;  
姓：&lt;input type="text" id="lname" size="20" /&gt;  
&lt;br /&gt;&lt;br /&gt;  
转换为大写  
&lt;input type="checkBox" onClick="if (this.checked) {convertToUcase()}"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
17  
使用单选按钮中的 value 属性  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function check(browser)  
  {  
  document.getElementById("answer").value=browser  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;您喜欢哪款浏览器？&lt;/p&gt;  
&lt;form&gt;  
&lt;input type="radio" name="browser" onClick="check(this.value)" value="Internet Explorer"&gt;Internet Explorer&lt;br /&gt;  
&lt;input type="radio" name="browser" onClick="check(this.value)" value="Firefox"&gt;Firefox&lt;br /&gt;  
&lt;input type="radio" name="browser" onClick="check(this.value)" value="Netscape"&gt;Netscape&lt;br /&gt;  
&lt;input type="radio" name="browser" onClick="check(this.value)" value="Opera"&gt;Opera&lt;br /&gt;  
&lt;br /&gt;  
您喜欢的浏览器是：&lt;input type="text" id="answer" size="20"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
18  
重置表单  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function formReset()  
{  
document.getElementById("myForm").reset()  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;在下面的文本框中输入一些文本，然后点击重置按钮就可以重置表单。&lt;/p&gt;  
&lt;form id="myForm"&gt;  
姓名：&lt;input type="text" size="20"&gt;&lt;br /&gt;  
年龄：&lt;input type="text" size="20"&gt;&lt;br /&gt;  
&lt;br /&gt;  
&lt;input type="button" onClick="formReset()" value="重置"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
19验证表单  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function validate()  
{  
var at=document.getElementById("email").value.indexOf("@")  
var age=document.getElementById("age").value  
var fname=document.getElementById("fname").value  
submitOK="true"  
if (fname.length&gt;10)  
{  
alert("名字必须小于 10 个字符。")  
submitOK="false"  
}  
if (isNaN(age)||age&lt;1||age&gt;100)  
{  
alert("年龄必须是 1 与 100 之间的数字。")  
submitOK="false"  
}  
if (at==-1)  
{  
alert("不是有效的电子邮件地址。")  
submitOK="false"  
}  
if (submitOK=="false")  
{  
return false  
}  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form action="/example/hdom/hdom\_submitpage.html" onSubmit="return validate()"&gt;  
名字（最多 10 个字符）：&lt;input type="text" id="fname" size="20"&gt;&lt;br /&gt;  
年龄（从 1 到 100）：&lt;input type="text" id="age" size="20"&gt;&lt;br /&gt;  
电子邮件：&lt;input type="text" id="email" size="20"&gt;&lt;br /&gt;  
&lt;br /&gt;  
&lt;input type="submit" value="提交"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
20 选取文本域中的内容  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function selText()  
{  
document.getElementById("myText").select()  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
&lt;input size="25" type="text" id="myText" value="选定我吧！"&gt;  
&lt;input type="button" value="选择文本" onClick="selText()"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
21\. 表单中的下拉列表  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function favBrowser()  
{  
var mylist=document.getElementById("myList")  
document.getElementById("favorite").value=mylist.options\[mylist.selectedIndex].text  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
请选择您喜欢的浏览器：  
&lt;select id="myList" onChange="favBrowser()"&gt;  
  &lt;option&gt;Internet Explorer&lt;/option&gt;  
  &lt;option&gt;Netscape&lt;/option&gt;  
  &lt;option&gt;Opera&lt;/option&gt;  
&lt;/select&gt;  
&lt;p&gt;您喜欢的浏览器是：&lt;input type="text" id="favorite" size="20"&gt;&lt;/p&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
22\.  
另一个下拉列表  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function moveNumbers()  
{  
var no=document.getElementById("no")  
var option=no.options\[no.selectedIndex].text  
var txt=document.getElementById("result").value  
txt=txt + option  
document.getElementById("result").value=txt  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
请选择数字：&lt;br /&gt;  
&lt;select id="no"&gt;  
&lt;option&gt;0&lt;/option&gt;  
&lt;option&gt;1&lt;/option&gt;  
&lt;option&gt;2&lt;/option&gt;  
&lt;option&gt;3&lt;/option&gt;  
&lt;option&gt;4&lt;/option&gt;  
&lt;option&gt;5&lt;/option&gt;  
&lt;option&gt;6&lt;/option&gt;  
&lt;option&gt;7&lt;/option&gt;  
&lt;option&gt;8&lt;/option&gt;  
&lt;option&gt;9&lt;/option&gt;  
&lt;/select&gt;  
&lt;input type="button" onClick="moveNumbers()" value="--&gt;"&gt;  
&lt;input type="text" id="result" size="20"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
23 当达到文本域的最大字符数时跳至下一个域  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function checkLen(x,y)  
{  
if (y.length==x.maxLength)  
{  
var next=x.tabIndex  
if (next&lt;document.getElementById("myForm").length)  
  {  
  document.getElementById("myForm").elements\[next].focus()  
  }  
}  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;这段脚本在达到文本框的最大长度时跳到下一个文本框：&lt;/p&gt;  
&lt;form id="myForm"&gt;  
&lt;input size="3" tabindex="1" maxlength="3" onKeyUp="checkLen(this,this.value)"&gt;  
&lt;input size="2" tabindex="2" maxlength="2" onKeyUp="checkLen(this,this.value)"&gt;  
&lt;input size="3" tabindex="3" maxlength="3" onKeyUp="checkLen(this,this.value)"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
23.重新加载文档  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function reloadPage()  
{  
window.location.reload();  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;input type="button" value="重新加载页面" onClick="reloadPage()" /&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
24.禁用并启用下拉列表  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function disable()  
  {  
  document.getElementById("mySelect").disabled=true  
  }  
function enable()  
  {  
  document.getElementById("mySelect").disabled=false  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
&lt;select id="mySelect"&gt;  
  &lt;option&gt;苹果&lt;/option&gt;  
  &lt;option&gt;桃子&lt;/option&gt;  
  &lt;option&gt;香蕉&lt;/option&gt;  
  &lt;option&gt;桔子&lt;/option&gt;  
&lt;/select&gt;  
&lt;br /&gt;&lt;br /&gt;  
&lt;input type="button" onClick="disable()" value="禁用列表"&gt;  
&lt;input type="button" onClick="enable()" value="启用列表"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
25 更改下拉列表中的可见行数  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function changeSize()  
  {  
  document.getElementById("mySelect").size=4  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
&lt;select id="mySelect"&gt;  
  &lt;option&gt;苹果&lt;/option&gt;  
  &lt;option&gt;桃子&lt;/option&gt;  
  &lt;option&gt;香蕉&lt;/option&gt;  
  &lt;option&gt;桔子&lt;/option&gt;  
&lt;/select&gt;  
&lt;input type="button" onClick="changeSize()" value="改变大小"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
26 选择下拉列表中的多个选项  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function selectMultiple()  
  {  
  document.getElementById("mySelect").multiple=true  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
&lt;select id="mySelect" size="4"&gt;  
  &lt;option&gt;苹果&lt;/option&gt;  
  &lt;option&gt;桃子&lt;/option&gt;  
  &lt;option&gt;香蕉&lt;/option&gt;  
  &lt;option&gt;桔子&lt;/option&gt;  
&lt;/select&gt;  
&lt;input type="button" onClick="selectMultiple()" value="选择多个"&gt;  
&lt;/form&gt;  
&lt;p&gt;在您点击 “选择多个” 按钮之前，请尝试同时选取多个选项。在点击 “选择多个” 按钮之后，请再试一次。&lt;/p&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
27  取得下拉列表中所选的选项的索引位置  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function alertIndex()  
  {  
  var x=document.getElementById("mySelect").selectedIndex;  
  var y=document.getElementsByTagName("option");  
  alert(y\[x].text + " has the index of: " + y\[x].index);  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
请选择您喜欢的水果：  
&lt;select id="mySelect"&gt;  
  &lt;option&gt;苹果&lt;/option&gt;  
  &lt;option&gt;桃子&lt;/option&gt;  
  &lt;option&gt;香蕉&lt;/option&gt;  
  &lt;option&gt;桔子&lt;/option&gt;  
&lt;/select&gt;  
&lt;br /&gt;  
&lt;br /&gt;  
&lt;input type="button" onClick="alertIndex()" value="显示被选水果的 index"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
28.从下拉列表中删除选项  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function removeOption()  
  {  
  var x=document.getElementById("mySelect")  
  x.remove(x.selectedIndex)  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form&gt;  
&lt;select id="mySelect"&gt;  
  &lt;option&gt;苹果&lt;/option&gt;  
  &lt;option&gt;桃子&lt;/option&gt;  
  &lt;option&gt;香蕉&lt;/option&gt;  
  &lt;option&gt;桔子&lt;/option&gt;  
&lt;/select&gt;  
&lt;input type="button" onClick="removeOption()" value="删除被选的选项"&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
29规定表格的内部边线  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function rowRules()  
  {  
  document.getElementById('myTable').rules="rows"  
  }  
function colRules()  
  {  
document.getElementById('myTable').rules="cols"  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;table id="myTable" border="1"&gt;  
&lt;tr&gt;  
&lt;td&gt;Row1 cell1&lt;/td&gt;  
&lt;td&gt;Row1 cell2&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;Row2 cell1&lt;/td&gt;  
&lt;td&gt;Row2 cell2&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;Row3 cell1&lt;/td&gt;  
&lt;td&gt;Row3 cell2&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;br /&gt;  
&lt;input type="button" onClick="rowRules()" value="仅显示行边框"&gt;  
&lt;input type="button" onClick="colRules()" value="仅显示列边框"&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
30为表格创建了一个标题  
&lt;html&gt;  
&lt;head&gt;  
&lt;script type="text/javascript"&gt;  
function createCaption()  
  {  
  var x=document.getElementById('myTable').createCaption()  
  x.innerHTML="我的表格标题"  
  }  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;table id="myTable" border="1"&gt;  
&lt;tr&gt;  
&lt;td&gt;Row1 cell1&lt;/td&gt;  
&lt;td&gt;Row1 cell2&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;Row2 cell1&lt;/td&gt;  
&lt;td&gt;Row2 cell2&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;br /&gt;  
&lt;input type="button" onClick="createCaption()" value="创建标题"&gt;  
&lt;/body&gt;  
&lt;/html&gt;