小白教程
所有教程
关于
Search
172.69.7.132
172.69.7.132
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Event stoppropagation”的源代码
本页内容
上一节:
Event_stopimmediatepropagation
下一节:
Jq_event_target
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:event.stopPropagation()}}[[Category:jquery ref events|22]] = jQuery event.stopPropagation() 方法 = [[jquery ref events|jQuery 事件方法]] 阻止 click 事件冒泡到父元素: <sample title="" desc="" lang="html" hererun="1"> $("span").click(function(event){ event.stopPropagation(); alert("The span element was clicked."); }); $("p").click(function(event){ alert("The p element was clicked."); }); $("div").click(function(){ alert("The div element was clicked."); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("span").click(function(event){ event.stopPropagation(); alert("The span element was clicked."); }); $("p").click(function(event){ alert("The p element was clicked."); }); $("div").click(function(){ alert("The div element was clicked."); }); }); </script> </head> <body> <div style="height:100px;width:500px;padding:10px;border:1px solid blue;background-color:lightblue;"> This is a div element. <p style="background-color:pink">This is a p element, in the div element. <br><span style="background-color:orange">This is a span element in the p and the div element.</span></p></div> <p><b>Note:</b> Click on each of the elements above. When clicking on the <b>div</b> element, it will alert that the div element was clicked. When clicking on the <b>p</b> element, it will return both the p and the div element, since the p element is inside the div element. But when clicking on the <b>span</b> element, it will only return itself, and not the p and the div element (even though its inside these elements). The event.stopPropagation() stops the click event from bubbling to the parent elements. </p> <p><b>Tip:</b> Try to remove the event.stopPropagation() line, and click on the span element again (the click event will now bubble up to parent elements).</p> </body> </html> </run> == 定义和用法 == event.stopPropagation() 方法阻止事件冒泡到父元素,阻止任何父事件处理程序被执行。 '''提示:'''请使用 [[event ispropagationstopped|event.isPropagationStopped()]] 方法来检查指定的事件上是否调用了该方法。 == 语法 == <pre> event.stopPropagation() </pre> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | event | 必需。event 参数来自事件绑定函数。 |} [[jquery ref events|jQuery 事件方法]]
返回至“
Event stoppropagation
”。
上一节:
Event_stopimmediatepropagation
下一节:
Jq_event_target