博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Javascript学习历程之事件
阅读量:5157 次
发布时间:2019-06-13

本文共 3868 字,大约阅读时间需要 12 分钟。

增加监听函数(f1)

方法一

window.onload = function () {
var myP = document.getElementById("myP"); myP.onclick = function () {
alert("我被点击了"); } }

方法二 添加处理函数attachEvent("onclick", fnClick),删除处理函数detachEvent("onclick", fnClick)

var myP; function fnClick() {
alert("我被点击了"); myP.detachEvent("onclick", fnClick); } window.onload = function () {
myP = document.getElementById("myP"); myP.attachEvent("onclick", fnClick); }

方法三 添加多个处理函数

var myP; function fnClick1() {
alert("我被点击了a"); } function fnClick2() {
alert("我被点击了b"); } window.onload = function () {
myP = document.getElementById("myP"); myP.attachEvent("onclick", fnClick1); myP.attachEvent("onclick", fnClick2); }

 

同一个函数处理多个事件(f2)

function handle(event) {
var divshow = document.getElementById("show"); if (event.type == "click") {
divshow.innerHTML = "你点击了我"; } else divshow.innerHTML = "你鼠标经过我"; } window.onload = function () {
var elem = document.getElementById("img-f2"); elem.onclick = handle; elem.onmouseover = handle; }

 

获取事件的目标(f2)event.target

function handle(event) {
var mydiv = document.getElementById("show"); mydiv.innerHTML = "事件的目标是" + event.target.tagName; } window.onload = function () {
var myElem = document.getElementById("img-f2"); myElem.onclick = handle; }

 

/*浏览器兼容性处理

function handle(oEvent){
    if(window.event) oEvent=window.event;
    var oTarget;
    if (oEvent.srcElement)
        oTarget = oEvent.srcElement;
    else
        oTarget=oEvent.target;
}
*/

控制鼠标事件(f2) onclick,ondblclick,onmouseover,onmouseout,onmouseup,onmousedown

function handle(event) {
var mydiv = document.getElementById("show"); mydiv.innerHTML+=event.type+"
"; } window.onload = function () {
var myElem = document.getElementById("img-f2"); myElem.onclick = handle; myElem.ondblclick = handle; myElem.onmouseover = handle; myElem.onmouseout = handle; myElem.onmouseup = handle; myElem.onmousedown = handle; }

 

鼠标button属性

function handle(event) {
var mydiv = document.getElementById("show"); mydiv.innerHTML ="button的值:"+ event.button; } document.οnmοusedοwn=handle; window.onload = handle;

 

键盘事件

例子1

function handle(event) {
var show = document.getElementById("show"); show.innerHTML += event.type+" "; } window.onload = function () {
var mytext = document.getElementById("mytext"); mytext.onkeydown = handle; mytext.onkeyup = handle; mytext.onkeypress = handle; }

例子2

function handle(event) {
var show = document.getElementById("show"); show.innerHTML += event.type + ":charCode: "+event.charCode+"keyCode"+event.keyCode+"
"; } window.onload = function () {
var mytext = document.getElementById("mytext"); mytext.onkeydown = handle; mytext.onkeyup = handle; mytext.onkeypress = handle; }

 

/*html事件

load
unload
error
select
change
submit
focus
blur
*/

屏蔽鼠标右键(f4)属性document.oncontextmenu 

function block(oEvent) {
var myP = document.getElementsByTagName("p")[0]; oEvent.returnValue = false; oEvent.preventDefault(); myP.innerHTML = "屏蔽鼠标右键"; if (window.event) {
oEvent = window.event;//兼容IE oEvent.returnValue = false; //取消默认事件 } else {
oEvent.preventDefault();//取消默认事件 } } document.oncontextmenu = block; window.onload = function () {
var oUL = document.getElementById("listUL"); var aLi = oUL.childNodes; var oA; for (var i = 0; i < aLi.length; i++) {
if (aLi[i].tagName == "LI" && aLi[i].getElementsByTagName("ul").length) {
oA = aLi[i].firstChild; oA.onclick = change; } } } function change() {
var oSecondDiv = this.parentNode.getElementsByTagName("ul")[0]; if (oSecondDiv.className == "myHide") oSecondDiv.className = "myShow"; else oSecondDiv.className = "myHide"; }

 

转载于:https://www.cnblogs.com/suguoqiang/archive/2012/02/26/2368763.html

你可能感兴趣的文章
kubernetes_book
查看>>
侧边栏广告和回到顶部
查看>>
https://blog.csdn.net/u012106306/article/details/80760744
查看>>
海上孤独的帆
查看>>
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
01: socket模块
查看>>
mysql触发器
查看>>
淌淌淌
查看>>
win10每次开机都显示“你的硬件设置已更改,请重启电脑……”的解决办法
查看>>
C++有关 const & 内敛 & 友元&静态成员那些事
查看>>
函数积累
查看>>
Swift 入门之简单语法(六)
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
bzoj1040: [ZJOI2008]骑士
查看>>
LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
查看>>
利用SignalR来同步更新Winfrom
查看>>