事件

一、事件:
1、注册事件的方式:
方式一: 直接在html元素上注册

方式二:可以使用js代码先找到对应的对象再注册。 推荐使用,原因:HTML代码和js代码分开,易于维护。

2、常用的事件:
1)鼠标点击相关:
a)onclick 在用户用鼠标左键单击对象时触发。
b)ondblclick 当用户双击对象时触发。
c)onmousedown 当用户用任何鼠标按钮单击对象时触发。
d)onmouseup 当用户在鼠标位于对象之上时释放鼠标按钮时触发。
2)鼠标移动相关:
a)onmouseout 当用户将鼠标指针移出对象边界时触发。
b)onmousemove 当用户将鼠标划过对象时触发。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script type="text/javascript">
    function clickTest(){
        alert("单击..");  
    }
    function dbClick(){
        alert("双击..");  
    }
    function showTime(){
        var timeSpan = document.getElementById("timeSpan");
        var date  = new Date().toLocaleString();
        timeSpan.innerHTML = date.fontcolor("red");
    }   
    function hideTime(){
        var timeSpan = document.getElementById("timeSpan");
        timeSpan.innerHTML = "";
    }
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
    <input type="button" onclick="clickTest()"  value="单击" />
    <input type="button" ondblclick="dbClick()" value="双击"/> 
    <span onmousemove="showTime()" onmouseout="hideTime()" >查看当前系统时间:</span><span id="timeSpan"></span>
</body></html>

3)焦点相关的:
a)onblur 在对象失去输入焦点时触发。
b)onfocus 当对象获得焦点时触发。
4)其他:
a)onchange 当对象或选中区的内容改变时触发。
b)onload 在浏览器完成对象的装载后立即触发。
c)onsubmit 当表单将要被提交时触发。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script type="text/javascript">
    function showInfo(){
        var timeSpan = document.getElementById("userName");
        timeSpan.innerHTML = "用户名是由6位的英文与数字组成".fontcolor("green");  
    }
    function hideInfo(){
        var timeSpan = document.getElementById("userName");
        timeSpan.innerHTML = "";    
    }
    function change(){
        alert("城市改变了..");   
    }
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
    用户名:<input type="text" onfocus="showInfo()"  onblur="hideInfo()"  /> <span id="userName"></span>
    <select onchange="change()" >
        <option>沈阳</option>
        <option>北京</option>
        <option>上海</option>
    </select>
</body></html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值