d-_*_*_-b 8 javascript jquery onclick parent-child
我如何停止onclick内部元素也点击进入其父元素.
<div id="1" onclick="alert('1')">
<div id="2" onclick="alert('2')"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果第二个div(id ="2")明显位于第一个div之上,并且单击它,则会同时获得警报1和2.
我怎样才能避免这种情况,所以我只能从我(明显)点击的内容中获得警报,而不会完全禁用外部div可能拥有的任何onclick功能.
?var outer = document.getElementById('outer');
outer.onclick = function(event)
{
alert('outer');
event.stopPropagation();
}??
var inner = document.getElementById('inner');
inner.onclick = function(event)
{
alert('inner');
event.stopPropagation();
}
Run Code Online (Sandbox Code Playgroud)
$("#inner").on("click", function(e){
e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1709 次 |
| 最近记录: |