考虑以下几点:
<div onclick="alert('you clicked the header')" class="header">
<span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>
我怎么能使它,当用户点击跨度,它不火div的点击事件?
考虑以下几点:
<div onclick="alert('you clicked the header')" class="header">
<span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>
我怎么能使它,当用户点击跨度,它不火div的点击事件?
当前回答
使用单独的处理程序,比如:
function myOnClickHandler(th){
//say let t=$(th)
}
在HTML中这样做:
<...onclick="myOnClickHandler(this); event.stopPropagation();"...>
甚至:
function myOnClickHandler(e){
e.stopPropagation();
}
for:
<...onclick="myOnClickHandler(event)"...>
其他回答
最好的解决方案是通过javascript函数处理事件,但为了使用一个简单而快速的解决方案,直接使用html元素,一旦“事件”和“窗口。(https://developer.mozilla.org/en-US/docs/Web/API/Window/event),我建议使用以下“硬代码”:
<div onclick="alert('blablabla'); (arguments[0] ? arguments[0].stopPropagation() : false);">...</div>
我也有同样的问题- IE中的js错误框-据我所见,这在所有浏览器中都很好(事件。cancelBubble=true在IE中工作)
onClick="if(event.stopPropagation){event.stopPropagation();}event.cancelBubble=true;"
这也可以工作-在链接HTML使用onclick返回像这样:
<a href="mypage.html" onclick="return confirmClick();">Delete</a>
然后,comfirmClick()函数应该像这样:
function confirmClick() {
if(confirm("Do you really want to delete this task?")) {
return true;
} else {
return false;
}
};
<div onclick="alert('you clicked the header')" class="header">
<span onclick="alert('you clicked inside the header'); event.stopPropagation()">
something inside the header
</span>
</div>
使用单独的处理程序,比如:
function myOnClickHandler(th){
//say let t=$(th)
}
在HTML中这样做:
<...onclick="myOnClickHandler(this); event.stopPropagation();"...>
甚至:
function myOnClickHandler(e){
e.stopPropagation();
}
for:
<...onclick="myOnClickHandler(event)"...>