嵌套的元素的点击事件
下面的代码 在tit1-1和tit1-2上点击时也会弹出 对话框 显示"tit1",有没有办法只在tit-1上点击上才alert?
<ul>
<li onclick="alert('tit1')">
tit1
<ul>
<li>tit1-1</li>
<li>tit1-2</li>
</ul>
</li>
</ul>
------解决方案--------------------最简单的方法单独加个标签
<ul>
<li>
<span onclick="alert('tit1')">tit1</span>
<ul>
<li>tit1-1</li>
<li>tit1-2</li>
</ul>
</li>
</ul>
------解决方案--------------------<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function ss(e){
var a=e
------解决方案--------------------window.event;
var src=a.srcElement
------解决方案--------------------a.target;
if(src.innerHTML==document.getElementsByTagName("li")[0].innerHTML){
alert("1");
}
}
window.onload=function(){
document.getElementsByTagName("li")[0].onclick=ss;
}
</script>
</head>
<body>
<ul>
<li>
tit1
<ul>
<li>tit1-1</li>
<li>tit1-2</li>
</ul>
</li>
</ul>
</body>
</html>
这样试试
------解决方案--------------------
<ul>
<li onclick="if(this==event.srcElement) alert('tit1')">
tit1
<ul>
<li>tit1-1</li>
<li>tit1-2</li>
</ul>
</li>
</ul>
------解决方案--------------------<ul>
<li onclick="alert('tit1')">
tit1
<ul
onclick="event.stopPropagation?event.stopPropagation():event.cancelBubble=true">
<li>tit1-1</li>
<li>tit1-2</li>
</ul>
</li>
</ul>
阻止冒泡