="text" name="myInput" size="20"> <input type="submit" value="Submit"> </form> </body>
</html>
对表单的检测
<html>
<head> <script type="text/javascript"> function validate() { x=document.myForm at=x.email.value.indexOf("@") code=x.code.value firstname=x.fname.value submitOK="True"
if (at==-1) { alert("Not a valid e-mail!") submitOK="False" } if (code<1 || code>5) { alert("The value must be between 1 and 5") submitOK="False" } if (firstname.length>10) { alert("Your name must be less than 10 characters") submitOK="False" } if (submitOK=="False") { return false } } </script> </head>
<body> <form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()"> Enter your e-mail: <input type="text" name="email" size="20"><br /> Enter a value from 1 to 5: <input type="text" name="code" size="20"><br /> Enter your name, max 10 chararcters: <input type="text" name="fname" size="20"><br /> <input type="submit" value="Submit"> </form> </body>
</html>
设置表单中的一项获得焦点
<html>
<head> <script type="text/javascript"> function setfocus() { document.forms[0].field.focus() } </script> </head>
<body> <form> <input type="text" name="field" size="30"> <input type="button" value="Set focus" onclick="setfocus()"> </form> </body>
</html>
选择文本框中的文本
<html>
<head> <script type="text/javascript"> function setfocus() { document.forms[0].txt.select() document.forms[0].txt.focus() } </script> </head>
<body> <form> <input type="text" name="txt" size="30" value="Hello World!"> <input type="button" value="Select text" onclick="setfocus()"> </form> </body>
</html>
下拉列表框的取值
<html> <head> <script type="text/javascript"> function put() { txt=document.forms[0].myList.options[document.forms[0].myList.selectedIndex].text document.forms[0].favorite.value=txt } </script> </head>
<body> <form> Select your favorite browser: <select name="myList" onchange="put()"> <option>Internet Explorer</option> <option>Netscape</option> <option>Opera</option> </select> <br /><br /> Your favorite browser is: <input type="text" name="favorite" size="20"> </form> </body>
</html>
单选按钮的取值
<html>
<head> <script type="text/javascript"> function check(browser) { document.forms[0].answer.value=browser } </script>
</head>
<body> <form> Select which browser is your favorite:<br /><br /> <input type="radio" name="browser" onclick="check(this.value)" value="Internet Explorer">Internet Explorer<br /> <input type="radio" name="browser" onclick="check(this.value)" value="Netscape">Netscape<br /> <input type="radio" name="browser" onclick="check(this.value)" value="Opera">Opera<br /> <br /> <input type="text" name="answer" size="20"> </form> </body>
</html>
下拉列表的值的显示
<html> <head> <script type="text/javascript"> function put() { option=document.forms[0].dropdown.options[document.forms[0].dropdown.selectedIndex].text txt=document.forms[0].number.value txt=txt + option document.forms[0].number.value=txt } </script> </head>
<body> <form> Select numbers:<br /> <select name="dropdown"> <option>0</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> </select> <input type="button" onclick="put()" value="-->"> <input type="text" name="number" size="20"> </form> </body>
</html>
下拉列表的连接
<html> <head> <script type="text/javascript"> function go(form) { location=form.selectmenu.value } </script> </head>
<body> <form> <select name="selectmenu" onchange="go(this.form)"> <option>--Select page--</option> <option value="Microsofthttp://www.microsoft.com">Microsoft</option> <option value="AltaVistahttp://www.altavista.com">AltaVista</option> <option value="W3Schoolshttp://www.w3schools.com">W3Schools</option> </select> </form> </body>
</html>
光标自动跳到下一个文本区
<html> <head> <script type="text/javascript"> function toUnicode(elmnt,content) { if (content.length==elmnt.maxLength) { next=elmnt.tabIndex if (next<document.forms[0].elements.length) { document.forms[0].elements[next].focus() } } } </script> </head>
<body> <p>This script automatically jumps to the next input field when the current field's maxlength has been reached.</p> <form> <input size="3" tabindex="1" maxlength="3" onkeyup="toUnicode(this,this.value)"> <input size="3" tabindex="2" maxlength="3" onkeyup="toUnicode(this,this.value)"> <input size="3" tabindex="3" maxlength="3" onkeyup="toUnicode(this,this.value)"> </form> </body>
</html>
Frame, Frameset 和 IFrame 对象
平分两个页面
<html> <frameset id="myFrameset" cols="50%,50%"> <frame id="leftFrame" src="frame_a_noresize.htm"> <frame id="rightFrame" src="frame_a.htm"> </frameset> </html>
<html> <head> <script type="text/javascript"> function disableResize() { parent.document.getElementById("leftFrame").noResize=true parent.document.getElementById("rightFrame").noResize=true } function enableResize() { parent.document.getElementById("leftFrame").noResize=false parent.document.getElementById("rightFrame").noResize=false } </script> </head>
<body bgcolor="#EFE7D6"> <form> <input type="button" onclick="disableResize()" value="No resize"> <input type="button" onclick="enableResize()" value="Resize"> </form> <p>Try to resize the frame.</p> <p>Right-click inside the two frames and select "View Source" to see the source code.</p> </body>
</html>
是否显示滚动条
<html> <frameset id="myFrameset" cols="50%,50%"> <frame id="leftFrame" src="frame_a_scrolling.htm"> <frame id="rightFrame" src="frame_a.htm"> </frameset> </html>
<html> <head> <script type="text/javascript"> function enableScrolling() { parent.document.getElementById("leftFrame").scrolling="yes" parent.document.getElementById("rightFrame").scrolling="yes" } function disableScrolling() { parent.document.getElementById("leftFrame").scrolling="no" parent.document.getElementById("rightFrame").scrolling="no" } </script> </head>
<body bgcolor="#EFE7D6"> <form> <input type="button" onclick="enableScrolling()" value="Scroll bars"> <input type="button" onclick="disableScrolling()" value="No scroll bars"> </form> <p>Right-click inside the frames and select "View Source" to see the source code.</p> </body>
</html>
改变页面的地址:
<html> <frameset id="myFrameset" cols="50%,50%"> <frame id="leftFrame" src="frame_a_src.htm"> <frame id="rightFrame" src="frame_a.htm"> </frameset> </html>
<html> <head> <script type="text/javascript"> function newSrc() { parent.document.getElementById("leftFrame").src="default.asp" parent.document.getElementById("rightFrame").src="http://www.w3schools.com" } </script> </head>
<body bgcolor="#EFE7D6"> <form> <input type="button" onclick="newSrc()" value="Change frame source"> </form> <p>Right-click inside the two frames and select "View Source" to see the source code.</p> </body>
</html>
跳出框架
<html> <head> <script type="text/javascript"> function breakout() { if (window.top!=window.self) { window.top.location="tryjs_breakout.htm" } } </script> </head>
<body> <form> Click the button to break out of the frame: <input type="button" onclick="breakout()" value="Break out!"> </form> </body>
</html>
更新两个页面
<html> <f 上一页 [1] [2] [3] [4] [5] [6] [7] 下一页 |