点击列表元素 - 问题 [英] clicking on list elements - problem

查看:51
本文介绍了点击列表元素 - 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我的清单:


< ul>

< li id = " A" onclick =" show(this)"> Aaaaaaaa< / li>

< li id =" b" onclick =" show(this)"> Bbbbbbbb< / li>

< li id =" c" onclick =" show(this)"> Cccccccc

< ul>

< li id =" d" onclick =" show(this)"> 111111< / li>

< li id =" e" onclick =" show(this)"> 222222< / li>

< li id =" f" onclick =" show(this)"> 333333

< ul>

< li id =" g" onclick =" show(this)"> @@@@@@@@@< / li>

< li id =" h" onclick =" show(this)"> {{{{{{{{}< / li>

< li id =" i" onclick =" show(this)"> ????>>>>>< / li>

< / ul>

< / li>

< / ul>

< / li>

< / ul>


< ul>

< li id =" j" onclick =" show(this)"> qqq< / li>

< li id =" k" onclick =" show(this)"> vvvv< / li>

< / ul>


功能:


功能显示(点击)

{

alert(clicked.id)

}


麻烦的是,当我点击id =" g"的列表元素时,

会逐一出现三个警告,显示g,然后是f ;和c。可以这么说因为这些元素是嵌套的。我怎么能阻止显示点击元素及其父母的id

?我只想点击元素的ID来显示




祝你好运,

ABS

Hi all.

My list:

<ul>
<li id="a" onclick="show(this)">Aaaaaaaa</li>
<li id="b" onclick="show(this)">Bbbbbbbb</li>
<li id="c" onclick="show(this)">Cccccccc
<ul>
<li id="d" onclick="show(this)">111111</li>
<li id="e" onclick="show(this)">222222</li>
<li id="f" onclick="show(this)">333333
<ul>
<li id="g" onclick="show(this)">@@@@@@@@@</li>
<li id="h" onclick="show(this)">{{{{{{{}</li>
<li id="i" onclick="show(this)">????>>>>></li>
</ul>
</li>
</ul>
</li>
</ul>

<ul>
<li id="j" onclick="show(this)">qqq</li>
<li id="k" onclick="show(this)">vvvv</li>
</ul>

And function:

function show(clicked)
{
alert(clicked.id)
}

The trouble is that when I click on the list element having id="g", there
appear three alerts one by one showing "g", then "f", and "c". Prabably
that''s because those element are nested. How could I prevent showing the id
of clicked element and its parents ? I''d like only clicked element''s id to
be showed.

Best regards,
ABS

推荐答案

到目前为止,我发现所有这些事件的最佳技巧来自Lasse

Reichstein Nielsen:

....

< li id =" i" onclick =" show(this,(event.target || event.srcElement))">一些

text *< / li>

... 。


函数show(src,trg){

if(src == trg){

alert(src.id) );

}

}


适用于IE 6.x和FF 1.0.3 / 4

The best trick for all this event mess so far I found is from Lasse
Reichstein Nielsen:
....
<li id="i" onclick="show(this, (event.target||event.srcElement))">Some
text*</li>
....

function show(src,trg) {
if (src == trg) {
alert(src.id);
}
}

Works for IE 6.x and FF 1.0.3/4


>到目前为止我发现的所有这些事件最好的伎俩是来自Lasse
> The best trick for all this event mess so far I found is from Lasse
Reichstein Nielsen:[...]
Reichstein Nielsen: [...]




这样有效!谢谢!


ABS



That works ! Thank you !

ABS


abs写道:
大家好。

我的清单:

< ul>
< li id =" a" onclick =" show(this)"> Aaaaaaaa< / li>
< li id =" b" onclick =" show(this)"> Bbbbbbbb< / li>
< li id =" c" onclick =" show(this)"> Cccccccc
< ul>
< li id =" d" onclick =" show(this)"> 111111< / li>
< li id =" e" onclick =" show(this)"> 222222< / li>
< li id =" f" onclick =" show(this)"> 333333
< ul>
< li id =" g" onclick =" show(this)"> @@@@@@@@@< / li>
< li id =" h" onclick =" show(this)"> {{{{{{{{}< / li>
< li id =" i" onclick =" show(this)"> ????>>>>>< / li>
< / ul>
< / li>
< / ul>
< / li>
< / ul>
< ul>
< li id =" j" ; onclick =" show(this)"> qqq< / li>
< li id =" k" onclick =" show(this)"> vvvv< / li>
< / ul>

和功能:

功能展示(点击)
{
警告(clicked.id)
}

麻烦的是当我点击id =" g"的列表元素时,
一个接一个地出现三个警告,显示g,然后是f,和c。
可怜的是因为那些元素是嵌套的。我怎么能阻止显示
点击元素及其父元素的ID?我只想点击元素'
id来展示。

最好的问候,
ABS
Hi all.

My list:

<ul>
<li id="a" onclick="show(this)">Aaaaaaaa</li>
<li id="b" onclick="show(this)">Bbbbbbbb</li>
<li id="c" onclick="show(this)">Cccccccc
<ul>
<li id="d" onclick="show(this)">111111</li>
<li id="e" onclick="show(this)">222222</li>
<li id="f" onclick="show(this)">333333
<ul>
<li id="g" onclick="show(this)">@@@@@@@@@</li>
<li id="h" onclick="show(this)">{{{{{{{}</li>
<li id="i" onclick="show(this)">????>>>>></li>
</ul>
</li>
</ul>
</li>
</ul>

<ul>
<li id="j" onclick="show(this)">qqq</li>
<li id="k" onclick="show(this)">vvvv</li>
</ul>

And function:

function show(clicked)
{
alert(clicked.id)
}

The trouble is that when I click on the list element having id="g", there appear three alerts one by one showing "g", then "f", and "c". Prabably that''s because those element are nested. How could I prevent showing the id of clicked element and its parents ? I''d like only clicked element''s id to be showed.

Best regards,
ABS




可能想考虑一种更全球化的方法来处理那些

点击......


<!DOCTYPE HTML PUBLIC" - // W3C / / DTD HTML 4.01 // EN"

" http://www.w3.org/TR/html4/strict.dtd">

< html>

< head>

< style type =" text / css">


li {

背景:#ddd;

保证金:2px 0;

光标:指针;

}


< / style>

< script type =" text / javascript">


document.onclick = function( e)

{

var tgt;

if((e = e || window.event)

&&(tgt = e.srcElement || e.target)

&& /LI/i.test(tgt.nodeName))

{

alert(tgt.id);

}

}


< / script>

< / head>

< body>

< ul>

< li id =" a" > Aaaaaaaa< / li>

< li id =" b"> Bbbbbbbb< / li>

< li id =" c"> Cccccccc

< ul>

< li id =" d"> 111111< / li>

< li id = " e"> 222222< / li>

< li id =" f"> 333333

< ul>

< li id =" g"> @@@@@@@@@< / li>

< li id =" h"> {{{{{ {{}< / li>

< li id =" i"> ????>>>>>< / li>

< / ul>

< / li>

< / ul>

< / li>

< / ul>

< ul>

< li id =" j"> qqq< / li>

< li id =" k"> vvvv< / li>

< / ul>

< / body>

< / html>



Might want to consider a more global approach to handling those
clicks...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

li {
background: #ddd;
margin: 2px 0;
cursor: pointer;
}

</style>
<script type="text/javascript">

document.onclick = function(e)
{
var tgt;
if ((e = e || window.event)
&& (tgt = e.srcElement || e.target)
&& /LI/i.test(tgt.nodeName))
{
alert(tgt.id);
}
}

</script>
</head>
<body>
<ul>
<li id="a">Aaaaaaaa</li>
<li id="b">Bbbbbbbb</li>
<li id="c">Cccccccc
<ul>
<li id="d">111111</li>
<li id="e">222222</li>
<li id="f">333333
<ul>
<li id="g">@@@@@@@@@</li>
<li id="h">{{{{{{{}</li>
<li id="i">????>>>>></li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li id="j">qqq</li>
<li id="k">vvvv</li>
</ul>
</body>
</html>


这篇关于点击列表元素 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆