<head>
<title> U </title>
</head>
<body>
<div id='d1' onclick="a(this.id)">1
<div id='d2' onclick="a(this.id)">2
<div id='d3' onclick="a(this.id)">3
</div>
</div>
</div>
<script>
function a(obj)
{
alert(document.getElementById(obj).innerText.substring(0,1));
window.event.cancelBubble = true;
}
</script>
</body>
</html>
<title> U </title>
</head>
<body>
<div id='d1' onclick="a(this.id)">1
<div id='d2' onclick="a(this.id)">2
<div id='d3' onclick="a(this.id)">3
</div>
</div>
</div>
<script>
function a(obj)
{
alert(document.getElementById(obj).innerText.substring(0,1));
window.event.cancelBubble = true;
}
</script>
</body>
</html>
@liuyann
能不能说说
alert(document.getElementById(obj).innerText.substring(0,1));
window.event.cancelBubble = true;
是什么意思呢,准备结帖了
这个貌似不对喔.用到了substring.
substring()取子字符串window.event.cancelBubble=true;
Bubbling is disabled for this event, preventing the next event handler in the hierarchy from receiving the event.
<div id='d1' onclick="a(this.id)">1
<div id='d2' onclick="a(this.id)">2
<div id='d3' onclick="a(this.id)">3
</div>
</div>
这里面的123只是我的测试数据,恰好让substring(0,1)钻了空子也是我需求没有表达明确.也许数据会是<div id='d1' onclick="a(this.id)">1
<div id='d2' onclick="a(this.id)">12
<div id='d3' onclick="a(this.id)">a3
</div>
</div>不过基本上解决了一些问题.感谢liuyann
<title> U </title>
</head>
<body>
<div id='d1' onclick="a(this.id)">1a
<div id='d2' onclick="a(this.id)">2bbb
<div id='d3' onclick="a(this.id)">3d
</div>
</div>
</div>
<script>
function a(obj)
{
alert(document.getElementById(obj).innerText.split("\n")[0]);
window.event.cancelBubble = true;
}
</script>
</body>
</html>
<div id=a1 onclick=xxx(this)>111
<div id=a2 onclick=xxx(this)>222
<div id=a3 onclick=xxx(this)>333</div>
</div></div><script>function xxx(o)
{
if(o!=event.srcElement)
{
return;
}
else
{
alert(event.srcElement.id);
}
}
</script>