Thực hành Java Script

19 727 2
Thực hành Java Script

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Thực hành Java Script

<html><body><script type="text/javascript">{document.write("<h1>This is a heading</h1>");document.write("<p>This is a paragraph.</p>");document.write("<p>This is another paragraph.</p>");}</script></body></html>JavaScript Popup Boxes<html><head><script type="text/javascript">function show_alert(){alert("I am an alert box!");}</script></head><body><input type="button" onclick="show_alert()" value="Show alert box" /></body></html><html><head><script type="text/javascript">function disp_alert(){alert("Hello again! This is how we" + '\n' + "add line breaks to an alert box!");}</script></head><body><input type="button" onclick="disp_alert()" value="Display alert box" /></body></html>1 <head><script type="text/javascript">function show_confirm(){var r=confirm("Press a button");if (r==true) { document.write("You pressed OK!"); }else { document.write("You pressed Cancel!"); }}</script></head><body><input type="button" onclick="show_confirm()" value="Show a confirm box" /></body></html>Function<html> <head> <script type="text/javascript"> function myfunction(txt) { alert(txt);} </script> </head> <body> <form action= “dia chi trang web”method= “GET”> <input type="button" onclick="myfunction('Good Morning!')" value="In the Morning"> <input type="button" onclick="myfunction('Good Evening!')" value="In the Evening"> </form> <p>When you click on one of the buttons, a function will be called. The function will alertthe argument that is passed to it.</p></body> </html> 2 Event ObjectWhich mouse button was clicked?<html><head><script type="text/javascript">function whichButton(event){if (event.button==2) { alert("You clicked the right mouse button!"); }else { alert("You clicked the left mouse button!"); }}</script></head><body onmousedown="whichButton(event)"><p>Click in the document. An alert box will alert which mouse button you clicked.</p></body></html>What is the keycode of the key pressed?<html><head><script type="text/javascript">function whichButton(event){alert(event.keyCode);}</script></head><body onkeyup="whichButton(event)"><p><b>Note:</b> Make sure the right frame has focus when trying this example!</p><p>Press a key on your keyboard. An alert box will alert the keycode of the key.</p></body></html>What are the coordinates of the cursor?<html><head><script type="text/javascript">function show_coords(event){x=event.clientX;y=event.clientY;3 alert("X coords: " + x + ", Y coords: " + y);}</script></head><body onmousedown="show_coords(event)"><p>Click in the document. An alert box will alert the x and y coordinates of the mouse pointer.</p></body></html>View and change the action URL of a form<html><head><script type="text/javascript">function changeAction(){var x=document.getElementById("myForm");alert("Original action: " + x.action);x.action="default.asp";alert("New action: " + x.action);x.submit();}</script></head><body><form id="myForm" action="js_examples.asp">Name: <input type="text" name=”t1” value="Mickey Mouse" /><input type="button" onclick="changeAction()"value="Change action attribute and submit form" /></form></body></html>View the method that is to be used when sending form data<html>4 <head><script type="text/javascript">function showMethod(){var x=document.getElementById("myForm");alert(x.method);}</script></head><body><form id="myForm" method="post">Name: <input type="text" size="20" value="Mickey Mouse" /><input type="button" onclick="showMethod()" value="Show method" /></form></body></html>Alert id, type, and value of a Button object + disable button<html><head><script type="text/javascript">function alertId(){var txt="Id: " + document.getElementById("myButton").id;txt=txt + ", type: " + document.getElementById("myButton").type;txt=txt + ", value: " + document.getElementById("myButton").value;alert(txt);document.getElementById("myButton").disabled=true;}</script></head><body><form name=“f1”><input type="button" value="Click me!" id="myButton" onclick="alertId()" /></form></body></html>Check and uncheck a checkbox<html><head>5 <script type="text/javascript">function check(){//document.getElementById("myCheck").checked=true;document.f1.uncheck.checked=true;}function uncheck(){//document.getElementById("myCheck").checked=false;document.f1.check.checked=false;}</script></head><body><form name= “f1”><input type="checkbox" id="myCheck" /><input type="button" onclick="check()" value="Check Checkbox" name= “check”/><input type="button" onclick="uncheck()" value="Uncheck Checkbox" name= “uncheck”/></form></body></html>Checkboxes in a form<html><head><script type="text/javascript">function createOrder(){coffee=document.forms[0].coffee;txt="";for (i=0;i<coffee.length;++ i) { if (coffee[i].checked!=0) { txt=txt + coffee[i].value + " "; } }document.getElementById("order").value="You ordered a coffee with " + txt;}</script></head><body><p>How would you like your coffee?</p><form><input type="checkbox" name="coffee" value="cream">With cream<br /><input type="checkbox" name="coffee" value="sugar">With sugar<br />6 <br /><input type="button" onclick="createOrder()" value="Send order"><br /><br /><input type="text" id="order" size="50"></form></body></html>Checkbox<html><head><script type="text/javascript">function convertToUcase(){document.getElementById("fname").value=document.getElementById("fname").value.toUpperCase();document.getElementById("lname").value=document.getElementById("lname").value.toUpperCase();}</script></head><body><form name="form1">First name: <input type="text" id="fname" size="20" /><br /><br />Last name: <input type="text" id="lname" size="20" /><br /><br />Convert to upper case <input type="checkBox" onclick="if (this.checked) {convertToUcase()}"></form></body></html>Radio buttons<html><head><script type="text/javascript">function check(browser) { document.getElementById("answer").value=browser; }</script></head><body><p>What's your favorite browser?</p><form>7 <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="Firefox">Firefox<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 />Your favorite browser is: <input type="text" id="answer" size="20"></form></body></html>Reset a form<html><head><script type="text/javascript">function formReset(){document.getElementById("myForm").reset();}</script></head><body><p>Enter some text in the text fields below, and then press the "Reset" button to reset the form.</p><form id="myForm">Name: <input type="text" size="20"><br />Age: <input type="text" size="20"><br /><br /><input type="button" onclick="formReset()" value="Reset"></form></body></html>Submit a form<html><head><script type="text/javascript">function formSubmit(){document.getElementById("myForm").submit();}</script></head><body><p>Enter some text in the text fields below, and then press the "Submit" button to submit the form.</p><form id="myForm" action="js_form_action.asp" method="get">8 Firstname: <input type="text" name="firstname" size="20"><br />Lastname: <input type="text" name="lastname" size="20"><br /><br /><input type="button" onclick="formSubmit()" value="Submit"></form></body></html>Form validation<html><head><script type="text/javascript">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>10) { alert("The name may have no more than 10 characters"); submitOK="false"; }if (isNaN(age)||age<1||age>100) { alert("The age must be a number between 1 and 100"); submitOK="false"; }if (at==-1) { alert("Not a valid e-mail!"); submitOK="false"; }if (submitOK=="false") { return false; }}</script></head><body><form action="tryjs_submitpage.htm" onsubmit="return validate()">Name (max 10 characters): <input type="text" id="fname" size="20"><br />Age (from 1 to 100): <input type="text" id="age" size="20"><br />E-mail: <input type="text" id="email" size="20"><br /><br /><input type="submit" value="Submit"> </form>9 </body></html>Set focus to an input field when the page loads<html><head><script type="text/javascript">function setFocus(){document.getElementById("fname").focus();}</script></head><body onload="setFocus()"><form>Name: <input type="text" id="fname" size="30"><br />Age: <input type="text" id="age" size="30"> </form></body></html>Select the content of an input field<html><head><script type="text/javascript">function selText(){document.getElementById("myText").select();}</script></head><body><form><input size="25" type="text" id="myText" value="A cat played with a ball"><input type="button" value="Select text" onclick="selText()"> </form></body></html>10 [...]... paragraph.</p>"); } < /script& gt; </body> </html> JavaScript Popup Boxes <html> <head> < ;script type="text/javascript"> function show_alert() { alert("I am an alert box!"); } < /script& gt; </head> <body> <input type="button" onclick="show_alert()" value="Show alert box" /> </body> </html> <html> <head> < ;script type="text/javascript"> function... /> </form> 13 < ;script type="text/javascript"> document.write(document.getElementById("mySelect").form.id); < /script& gt; </p> </body> </html> Get the number of options in the dropdown list <html> <head> < ;script type="text/javascript"> function getLength() { alert(document.getElementById("mySelect").length); } < /script& gt; </head> <body> <form> <select... list <html> <head> < ;script type="text/javascript"> function getOptions() { var x=document.getElementById("mySelect"); txt="All options: " for (i=0;i<x.length;i++) { txt=txt + "\n" + x.options[i].text; } alert(txt); } < /script& gt; </head> <body> <form> 16 < ;script type="text/javascript"> function check() { //document.getElementById("myCheck").checked=true; document.f1.uncheck.checked=true; } function... event <html> <head> < ;script type="text/javascript"> function startTime() { var today=new Date(); 18 <html> <body> < ;script type="text/javascript"> { document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); } < /script& gt; </body> </html> JavaScript... list <html> <head> < ;script type="text/javascript"> function changeSize() { document.getElementById("mySelect").size=4; } < /script& gt; </head> <body> <form> <select id="mySelect"> <option>Apple</option> <option>Banana</option> <option>Orange</option> <option>Melon</option> 15 <head> < ;script type="text/javascript"> function... form <html> <head> < ;script type="text/javascript"> function createOrder() { coffee=document.forms[0].coffee; txt=""; for (i=0;i<coffee.length;++ i) { if (coffee[i].checked!=0) { txt=txt + coffee[i].value + " "; } } document.getElementById("order").value="You ordered a coffee with " + txt; } < /script& gt; </head> <body> <p>How... /> 6 Event Object Which mouse button was clicked? <html> <head> < ;script type="text/javascript"> function whichButton(event) { if (event.button==2) { alert("You clicked the right mouse button!"); } else { alert("You clicked the left mouse button!"); } } < /script& gt; </head> <body onmousedown="whichButton(event)"> <p>Click... will alert which mouse button you clicked.</p> </body> </html> What is the keycode of the key pressed? <html> <head> < ;script type="text/javascript"> function whichButton(event) { alert(event.keyCode); } < /script& gt; </head> <body onkeyup="whichButton(event)"> <p><b>Note:</b> Make sure the right frame has focus when... size"> </form> </body> </html> Select multiple options in a dropdown list <html> <head> < ;script type="text/javascript"> function selectMultiple() { document.getElementById("mySelect").multiple=true; } < /script& gt; </head> <body> <form> <select id="mySelect" size="4"> <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option> </select> <input... option"> </form> </body> </html> Remove options from a dropdown list <html> <head> < ;script type="text/javascript"> function removeOption() { var x=document.getElementById("mySelect"); x.remove(x.selectedIndex); } < /script& gt; </head> <body> <form> <select id="mySelect"> <option>Apple</option> . paragraph.</p>");}< /script& gt;</body></html>JavaScript Popup Boxes<html><head>< ;script type="text/javascript">function. /></body></html>Function<html> <head> < ;script type="text/javascript"> function myfunction(txt) { alert(txt);} < /script& gt; </head> <body>

Ngày đăng: 19/09/2012, 09:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan