表:点击的行 [英] Table: clicked row

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

问题描述

有没有办法可以获得类似


< table onclick =" f(this)">

< tr> < td> ...

< tr>< td> ...

....

< / table> ;


f()

{

row = getClickedRow(xyz)

alert(''你点击了行没有''+行)

}


而不是将onclick事件设置为每个< tr>?

-

mailto:da ******* @ yahoo.com( www.kezele.com

ICQ#:129068041 IRC#:piglu linux

Ako ne ide drukcije onda probaj posteno。

Is there a way to have something like

<table onclick="f(this)">
<tr><td>...
<tr><td>...
....
</table>

f()
{
row = getClickedRow(xyz)
alert(''You clicked row no '' + row)
}

instead of setting onclick event to every <tr>?
--
mailto:da*******@yahoo.com (www.kezele.com)
ICQ#: 129068041 IRC#: piglu linux
Ako ne ide drukcije onda probaj posteno.

推荐答案




几种简单的方法:

function checkrow(ev){

obj =(window.external)? event.srcElement:ev.target;

if(/tr/i.test(obj.nodeName))

alert(''嘿!你点了一行'' );

}

document.onclick = checkrow;

-----------------

另一种方式是给表和ID并使用事件处理程序遍历其

rows []集合:


MYTABLE = document.getElementById(''SOMETABLE'');

for(i = 0; i< MYTABLE.rows.length; i ++){


MYTABLE.rows [i] .onclick = ......;

}


Danny

On Sun,03 2005年7月16:55:01 -0700,Dalibor Kezele< da ******* @ yahoo.com>

写道:


- -

使用Opera革命性的电子邮件客户端: http ://www.opera.com/mail/


Dalibor Kezele写道:
Dalibor Kezele wrote:
有没有办法有类似的东西

< table onclick =" f (this)">
< tr>< td> ...
< tr>< td> ...
...
< / table>

f()
{= / row = getClickedRow(xyz)
alert(''你点击行没有''+行)
}

而不是为每个< tr>设置onclick事件?
Is there a way to have something like

<table onclick="f(this)">
<tr><td>...
<tr><td>...
...
</table>

f()
{
row = getClickedRow(xyz)
alert(''You clicked row no '' + row)
}

instead of setting onclick event to every <tr>?




你可以使用脚本为每一行添加一个onclick,方式你不要
必须把它放在HTML中,或者使用以下内容。


有关事件模型的解释(它支持两者) IE和

其他人),请阅读Quirksmode:


< URL:http://www.quirksmode.org/js/introevents.html> ;


简而言之,传递''event''会引用

点击的元素。 IE使用全局''window.event''因此在获得事件时考虑到

。 ''event''引用了点击的节点 - IE使用''srcElement'',其他人使用''target''。由于这个
几乎肯定会引用一个单元格而不是行或表格,所以将父节点的
向上移动到第一个父行,这就是它。


< table onclick =" showRowIdx(event,this)">

< tr>< td> row 0< / td>< ; / tr>

< tr>< td>第1行< / td>< / tr>

< tr>< td>第2行< / td>< / tr>

< tr>< td>第3行< / td>< / tr>

< / table>


< script type =" text / javascript">

function showRowIdx(e,t){

e = e | | window.event;

var tgt = e.target || e.srcElement;

while(''tr''!= tgt.nodeName.toLowerCase()&& tgt.parentNode){

tgt = tgt.parentNode

}

alert(''你点击行''+ tgt.rowIndex);

}

< / script>

-

Rob



You could add an onclick to each row using a script, that way you don''t
have to put it in the HTML, or use the following.

For an explanation of the event model (it supports both IE and
others), have a read at Quirksmode:

<URL:http://www.quirksmode.org/js/introevents.html>

In brief, passing ''event'' gives a reference to the element that was
clicked on. IE uses a global ''window.event'' so take account of that
when getting the event. ''event'' has a reference to the node that was
clicked on - IE uses ''srcElement'', others use ''target''. Since this
will almost certainly refer to a cell rather than the row or table, go
up parentNodes to the first parent row, and that''s it.

<table onclick="showRowIdx(event,this)">
<tr><td>row 0</td></tr>
<tr><td>row 1</td></tr>
<tr><td>row 2</td></tr>
<tr><td>row 3</td></tr>
</table>

<script type="text/javascript">
function showRowIdx(e, t){
e = e || window.event;
var tgt = e.target || e.srcElement;
while ( ''tr'' != tgt.nodeName.toLowerCase() && tgt.parentNode ) {
tgt = tgt.parentNode
}
alert(''You click on row '' + tgt.rowIndex);
}
</script>
--
Rob


Danny写道:


几种简单的方法:

函数checkrow(ev){
obj =(window.external)? event.srcElement:ev.target;
if(/tr/i.test(obj.nodeName))
alert(''嘿!你点了一行'');
}


A couple of simple ways:
function checkrow(ev) {
obj=(window.external) ? event.srcElement : ev.target;
if (/tr/i.test(obj.nodeName))
alert(''hey! you clicked a row'');
}




你测试过吗?它永远不会返回对行的引用,除非

onclick在行上,这是OP想要避免的。


请求还包括知道哪一行被点击了,而不仅仅是

任何一行。


[...]

-

Rob



Did you test that? It will never return a reference to a row unless
the onclick is on the row, which is what the OP wants to avoid.

The request also included knowing which row was clicked on, not just
any row.

[...]
--
Rob


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

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