Javascript语法(不断更新)

来源:互联网 发布:当前java文件的路径 编辑:程序博客网 时间:2024/05/19 17:49

1. document.getElementById()
含义:根据页面上控件Id 获取控件对象元素的ID特性
例如有如下text控件元素:

<input type="text" id="button1" value="Hello"/>

那么当调用document.getElementById(“button1”).value的时候,返回的就是”Hello”了。

2. setTimeout
定义和用法: setTimeout()方法用于在指定的毫秒数后调用函数或计算表达式。  

语法: setTimeout(code,millisec)  

参数: code (必需):要调用的函数后要执行的 JavaScript 代码串。millisec(必需):在执行代码前需等待的毫秒数。   提示: setTimeout() 只执行 code 一次。如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout()。
如:

<script type="text/javascript">function timedMsg(){var t=setTimeout("alert('5 秒时间到!')",5000)}</script>

接下来我们看一下一个简单的计时器:

<html><head><script type="text/javascript">var c=0function timedCount(){document.getElementById('txt').value=cc=c+1setTimeout("timedCount()",1000)}</script></head><body><form><input type="button" value="开始计时!" onClick="timedCount()"><input type="text" id="txt"></form><p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p></body>

效果如下:
这里写图片描述

3.隐藏文本

<!DOCTYPE html><html><body><p id="p1">这是一段文本。</p><input type="button" value="隐藏文本" onclick="document.getElementById('p1').style.visibility='hidden'" /><input type="button" value="显示文本" onclick="document.getElementById('p1').style.visibility='visible'" /></body></html>

4.关于setInterval和clearInterval的用法
首先,setInterval是一个实现定时调用的函数,可按照指定的周期(以毫秒计,如1000就是1秒)来调用函数或计算表达式。setInterval方法会不停地调用函数,直到 clearInterval被调用或窗口被关闭。由setInterval返回的ID值可用作clearInterval方法的参数。
eg:

<html><body><input type="text" id="clock" size="35" /><script language=javascript>var int=setInterval("clock()",1000)function clock()  {  var t=new Date()  document.getElementById("clock").value=t  }</script><button onclick="int=clearInterval(int)">停止</button></body></html>

可看到结果:
这里写图片描述
此外有一点需要注意,clearInterval关闭setInterval的周期性调用函数是一一对应的,也就是说,用了多少次的setInterval函数,要关闭时就要调用多少次的clearInterval函数。

5.indexOf和lastIndexOf的用法
(1)indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

stringObject.indexOf(searchvalue,fromindex)

该方法将从头到尾地检索字符串 stringObject,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的开头(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一次出现的位置。stringObject 中的字符位置是从 0 开始的。
注释:indexOf() 方法对大小写敏感!
注释:如果要检索的字符串值没有出现,则该方法返回 -1。

(2)lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。如果找到一个 searchvalue,则返回 searchvalue 的第一个字符在 stringObject 中的位置。stringObject 中的字符位置是从 0 开始的。
注释:lastIndexOf() 方法对大小写敏感!
注释:如果要检索的字符串值没有出现,则该方法返回 -1。

应用:可用来对表单中的必填项进行验证(在数据被送往服务器前对 HTML 表单中的这些输入数据进行验证)。
eg:

<html><head><script type="text/javascript">function validate_form(thisform){with (thisform)  {  if (email.value==null||email.value=="")    {alert("Email must be filled out!");     email.focus();return false;    }    else{        var str=email.value;        var a=str.indexOf("@");        var b=str.lastIndexOf(".");        if(a<1 || (b-a)>1)        {            alert("Not a valid email!");return false;        }        else{        return true;}    }  }}</script></head><body><form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">Email: <input type="text" name="email" size="30"><input type="submit" value="Submit"> </form></body></html>

6.replace()方法
在字符串中用某些字符替换另一些字符,如果替换掉的话,就返回替换成功后的字符串

7.match()方法
查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

原创粉丝点击