小白教程
所有教程
关于
Search
172.70.127.162
172.70.127.162
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Met document addeventlistener”的源代码
本页内容
上一节:
Prop_document_activeelement
下一节:
Met_document_adoptnode
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:document.addEventListener()}}[[Category:dom obj document|2]] = HTML DOM addEventListener() 方法 = [[dom obj document|Document 对象]] 在文档中添加点击事件。当用户在文档任何地方点击时,在 id="demo" 的 <p> 元素上输出 "Hello World": <sample title="" desc="" lang="html" hererun="1"> document.addEventListener("click", function(){ document.getElementById("demo").innerHTML = "Hello World"; }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>该实例使用了 addEventListener() 方法向文档中添加点击事件。</p> <p>尝试点击文档的任何地方。</p> <p id="demo"></p> <script> document.addEventListener("click", function(){ document.getElementById("demo").innerHTML = "Hello World!"; }); </script> </body> </html> </run> == 定义与用法 == document.addEventListener() 方法用于向文档添加事件句柄。 '''提示:''' 可以使用 [[met document removeeventlistener|document.removeEventListener()]] 方法来移除 addEventListener() 方法添加的事件句柄。 '''提示:'''使用 [[met element addeventlistener|element.addEventListener()]] 方法为指定元素添加事件句柄。 == 浏览器支持 == 表格中的数字表示支持该方法的第一个浏览器的版本号。 {| class="table table-striped table-hover" ! 方法 ! Chrome ! Internet Explorer ! Firefox ! Safari ! Opera |- | addEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |} '''注意:''' Internet Explorer 8 及更早IE版本不支持 addEventListener() 方法,Opera 7.0 及 Opera 更早版本也不支持。 但是,对于这类浏览器版本可以使用 '''attachEvent()''' 方法来添加事件句柄(跨浏览器兼容问题可以查看"更多实例")。 == 语法 == document.addEventListener(event, function, useCapture) == 参数值 == {| class="table table-striped table-hover" !width="50%"| 参数 !width="50%"| 描述 |- | event | 必需。描述事件名称的字符串。'''注意:''' 不要使用 "on" 前缀。例如,使用 "click" 来取代 "onclick"。 '''提示:''' 所有 HTML DOM 事件,可以查看我们完整的 [[dom obj event|HTML DOM Event 对象参考手册]]。 |- | function | 必需。描述了事件触发后执行的函数。 当事件触发时,事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如, "click" 事件属于 MouseEvent(鼠标事件) 对象。 |- | useCapture | 可选。布尔值,指定事件是否 在捕获或冒泡阶段执行。可能值: * true - 事件句柄在捕获阶段执行 * false- 默认。事件句柄在冒泡阶段执行 |} == 技术细节 == {| class="table table-striped table-hover" | DOM 版本: | DOM Level 2 Events |- | 返回值: | 没有返回值 |- | 修改记录: | 在 Firefox 6 和 Opera 11.60 中 useCapture 参数是可选的。 (在 Chrome、IE 和 Safari 中一直都是可选的)。 |} == 更多实例 == 您可以通过函数名来引用外部函数: <sample title="" desc="" lang="html" hererun="1"> document.addEventListener("click", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "Hello World"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>在用户点击文档任意地方时使用 addEventListener() 方法执行指定函数。</p> <p id="demo"> <script> document.addEventListener("click", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "Hello World"; } </script> </body> </html> </run> 您可以在文档中添加许多事件,添加的事件不会覆盖已存在的事件。 该实例演示了如何在文档中添加两个点击事件: <sample title="" desc="" lang="html" hererun="1"> document.addEventListener("click", myFunction); document.addEventListener("click", someOtherFunction); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>使用 addEventListener() 方法来向文档添加点击事件。</p> <p>点击文档任意处。</p> <script> document.addEventListener("click", myFunction); document.addEventListener("click", someOtherFunction); function myFunction() { alert ("Hello World!") } function someOtherFunction() { alert ("该函数同样执行!") } </script> </body> </html> </run> 您可以在文档中添加不同类型的事件。 该实例演示了如何在文档中添加多个事件: <sample title="" desc="" lang="html" hererun="1"> document.addEventListener("mouseover", myFunction); document.addEventListener("click", someOtherFunction); document.addEventListener("mouseout", someOtherFunction); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>使用 the addEventListener() 方法向文档添加许多事件。</p> <p id="demo"></p> <script> document.addEventListener("mouseover", myFunction); document.addEventListener("click", mySecondFunction); document.addEventListener("mouseout", myThirdFunction); function myFunction() { document.getElementById("demo").innerHTML += "Moused over!<br>" } function mySecondFunction() { document.getElementById("demo").innerHTML += "Clicked!<br>" } function myThirdFunction() { document.getElementById("demo").innerHTML += "Moused out!<br>" } </script> </body> </html> </run> 当传递参数值时,使用"匿名函数"调用带参数的函数: <sample title="" desc="" lang="html" hererun="1"> document.addEventListener("click", function() { myFunction(p1, p2); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>实例演示了在使用 addEventListener() 方法时如何传递参数值。</p> <p>单击文档中的任意位置,执行计算。</p> <p id="demo"></p> <script> var p1 = 5; var p2 = 7; document.addEventListener("click", function() { myFunction(p1, p2); }); function myFunction(a, b) { var result = a * b; document.getElementById("demo").innerHTML = result; } </script> </body> </html> </run> 修改 <body> 元素的背景: <sample title="" desc="" lang="html" hererun="1"> document.addEventListener("click", function(){ document.body.style.backgroundColor = "red"; }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>点击文档任意处修改背景颜色。</p> <script> document.addEventListener("click", function(){ document.body.style.backgroundColor = "red"; }); </script> </body> </html> </run> 使用 removeEventListener() 方法移除通过 addEventListener() 方法添加的事件句柄: <sample title="" desc="" lang="html" hererun="1"> // 向文档添加事件句柄 document.addEventListener("mousemove", myFunction); // 移除文档的事件句柄 document.removeEventListener("mousemove", myFunction); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>文档添加 onmousemove 事件句柄,当在文档中移动鼠标时会显示随机数。</p> <p>点击按钮移除事件句柄。</p> <button onclick="removeHandler()">点我</button> <p id="demo"></p> <script> document.addEventListener("mousemove", myFunction); function myFunction() { document.getElementById("demo").innerHTML = Math.random(); } function removeHandler() { document.removeEventListener("mousemove", myFunction); } </script> </body> </html> </run> 如果浏览器不支持 addEventListener() 方法, 你可以使用 attachEvent() 方法替代。 以下实例演示了跨浏览器的解决方法: <sample title="" desc="" lang="html" hererun="1"> if (document.addEventListener) { //所有主流浏览器,除了 IE 8 及更早 IE版本 document.addEventListener("click", myFunction); } else if (document.attachEvent) { // IE 8 及更早 IE 版本 document.attachEvent("onclick", myFunction); } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p> Internet Explorer 8 及更早 IE 版本不支持 addEventListener() 方法 。</p> <p>该实例颜色了跨浏览器的解决方案。</p> <p>点击文档的任意地方会弹出信息。</p> <script> if (document.addEventListener) { document.addEventListener("click", myFunction); } else if (document.attachEvent) { document.attachEvent("onclick", myFunction); } function myFunction() { alert("Hello World!"); } </script> </body> </html> </run> == 相关页面 == JavaScript 教程:[[js htmldom eventlistener|HTML DOM EventListener]] JavaScript 参考手册:[[met element addeventlistener|element.addEventListener()]] [[dom obj document|Document 对象]]
返回至“
Met document addeventlistener
”。
上一节:
Prop_document_activeelement
下一节:
Met_document_adoptnode