| a string contains a specified word. If the word is found it returns the position of the first character of the word in the original string. Note: The first position in the string is 0!</p>
</body> </html>
检测子字符串是否存在的例子:
<html> <body>
<script type="text/javascript"> var str = "W3Schools is great!" document.write(str.match("great")) </script>
<p>This example tests if a string contains a specified word. If the word is found it returns the word.</p>
</body> </html>
取子字符串的例子:
<html> <body>
<script type="text/javascript"> var str="W3Schools is great!" document.write(str.substr(2,6)) document.write("<br /><br />") document.write(str.substring(2,6)) </script>
<p> The substr() method returns a specified part of a string. If you specify (2,6) the returned string will be from the second character (start at 0) and 6 long. </p>
<p> The substring() method also returns a specified part of a string. If you specify (2,6) it returns all characters from the second character (start at 0) and up to, but not including, the sixth character. </p>
</body> </html>
转换字符串的大小写
<html> <body>
<script type="text/javascript"> var str=("Hello JavaScripters!") document.write(str.toLowerCase()) document.write("<br>") document.write(str.toUpperCase()) </script>
</body> </html>
数组对象的实例
数组简单应用的例子:
<html> <body>
<script type="text/javascript"> var famname = new Array(6) famname[0] = "Jan Egil" famname[1] = "Tove" famname[2] = "Hege" famname[3] = "Stale" famname[4] = "Kai Jim" famname[5] = "Borge"
for (i=0; i<6; i++) { document.write(famname[i] + "<br>") } </script>
</body> </html>
另一种使用数组的方法:
<html> <body>
<script type="text/javascript"> var famname = new Array("Jan Egil","Tove","Hege","Stale","Kai Jim","Borge")
for (i=0; i<famname.length; i++) { document.write(famname[i] + "<br>") } </script>
</body> </html>
使用数组的一些属性和方法:
<html> <body>
<script type="text/javascript"> var famname = new Array(3) famname[0] = "Jani" famname[1] = "Tove" famname[2] = "Hege"
document.write(famname.length + "<br>") document.write(famname.join(".") + "<br>") document.write(famname.reverse() + "<br>") document.write(famname.push("Ola","Jon") + "<br>") document.write(famname.pop() + "<br>") document.write(famname.shift() + "<br>") </script>
</body> </html>
数组的两个方法concat和slice
<html> <body>
<script type="text/javascript"> var famname = new Array(3) famname[0] = "Jani" famname[1] = "Tove" famname[2] = "Hege"
var famname2 = new Array(3) famname2[0] = "John" famname2[1] = "Andy" famname2[2] = "Wendy"
var famname3 = new Array("Stale","Borge")
document.write(famname.join() + "<br>") document.write(famname.concat(famname2) + "<br>") document.write(famname.concat(famname2,famname3) + "<br>") document.write(famname.slice(1) + "<br>") </script>
</body> </html>
日期相关例子:
显示今天的日期:
<html> <body>
<script type="text/javascript"> var d = new Date() document.write(d.getDate()) document.write(".") document.write(d.getMonth() + 1) document.write(".") document.write(d.getFullYear()) </script>
</body> </html>
显示当前的时间:
<html> <body>
<script type="text/javascript"> var d = new Date() document.write(d.getHours()) document.write(".") document.write(d.getMinutes()) document.write(".") document.write(d.getSeconds()) </script>
</body> </html>
设置日期:
<html> <body>
<script type="text/javascript"> var d = new Date() d.setFullYear("1990") document.write(d) </script>
</body> </html>
UTC时间:
<html> <body>
<script type="text/javascript"> var d = new Date() document.write(d.getUTCHours()) document.write(".") document.write(d.getUTCMinutes()) document.write(".") document.write(d.getUTCSeconds()) </script>
</body> </html>
显示当前的星期:
<html> <body> <script type="text/javascript"> var d=new Date() var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") document.write("Today is " + weekday[d.getDay()]) </script> </body> </html>
显示当前的日期和星期:
<html> <body> <script type="text/javascript"> var d=new Date() var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") document.write(weekday[d.getDay()] + " ") document.write(d.getDate() + ". ") document.write(monthname[d.getMonth()] + " ") document.write(d.getFullYear()) </script> </body> </html>
一个走动的时间:
<html> <head> <script type="text/javascript"> var timer = null
function stop() { clearTimeout(timer) }
function start() { var time = new Date() var hours = time.getHours() var minutes = time.getMinutes() minutes=((minutes < 10) ? "0" : "") + minutes var seconds = time.getSeconds() seconds=((seconds < 10) ? "0" : "") + seconds var clock = hours + ":" + minutes + ":" + seconds document.forms[0].display.value = clock timer = setTimeout("start()",1000) } </script> </head> <body onload="start()" onunload="stop()"> <form> <input type="text" name="display" size="20"> </form> </body> </html>
数学对象的例子:
<html> <body>
<script type="text/javascript"> document.write(Math.round(7.25)) </script>
</body> </html>
产生0-1之间的随机数的例子
<html> <body>
<script type="text/javascript"> document.write(Math.random()) </script>
</body> </html>
产生0-10的随机数的例子
<html> <body>
<script type="text/javascript"> no=Math.random()*10 document.write(Math.round(no)) </script>
</body> </html>
求最大数的例子:
<html> <body>
<script type="text/javascript"> document.write(Math.max(2,4)) </script>
</body> </html>
求最小数的例子:
<html> <body>
<script type="text/javascript"> document.write(Math.min(2,4)) </script>
</body> </html>
Convert Celsius to Fahrenheit
<html> <head> <script type="text/javascript"> function convert(degree) { if (degree=="C") { F=document.myform.celsius.value * 9 / 5 + 32 document.myform.fahrenheit.value=Math.round(F) } else { C=(document.myform.fahrenheit.value -32) * 5 / 9 document.myform.celsius.value=Math.round(C) } } </script> </head> <body>
<b>Insert a number in either input field, and the number will be converted into either Celsius or Fahrenheit.</b> <br /> <form name="myform"> <input name="celsius" onkeyup="convert('C')"> degrees Celsius<br /> equals<br /> <input name="fahrenheit" onkeyup="convert('F')"> degrees Fahrenheit </form> <br /> Note that the <b>Math.round</b> method is used, so that the result will be returned as a whole number.
</body> </html>
转变字符为数字的例子
<html> <head>
<script type="text/javascript"> function toUnicode() { var str=document.myForm.myInput.value if (str!="") { unicode=str.charCodeAt(0) } document.myForm.unicode.value=unicode } </script> </head> <body>
<form name="myForm"> Write a character:<br /> <input size="1" name="myInput" maxlength="1" onkeyup="toUnicode()"> <hr /> The character's Unicode:<br /> <input size="3" name="unicode"> </form>
</html>
超级连接对象
用按钮来改变连接位置的例子:
<html>
<head> <script type="text/javascript"> function myHref() { document.getElementById('myAnchor').innerText="Visit W3Schools" document.getElementById('myAnchor').href="http://www.w3schools.com" } </script> </head>
<body> <a id="myAnchor" href="Visit'>http://www.microsoft.com">Visit Microsoft</a> <form> <input type="button" onclick="myHref()" value="Change URL and text"> </form> </body>
</html>
改变连接的打开方式的例子:
<html>
<head> <script type="text/javascript"> function myTarget() { document.getElementById('myAnchor').target="_blank" } </script> </head>
<body> <a id="myAnchor" href="Visit'> 上一页 [1] [2] [3] [4] [5] [6] [7] 下一页 |