菜鸟学习javascript实例教程(3)

2008-02-23 07:41:41来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

</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()])

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:网页制作中表单相关特效整理

下一篇:使用Javascript创建XML文件