获取点击行索引的索引

发布于 2024-11-27 02:58:40 字数 804 浏览 0 评论 0原文

我是 mootools 新手,但对 jQuery 有很好的了解,
我正在尝试获取点击的索引。
这是我的 HTML 代码..

<table>
    <tr><td>One</td><td>Two</td><td>Three</td></tr>
    <tr><td>Four</td><td>Five</td><td>Six</td></tr>
    <tr><td>Seven</td><td>Eight</td><td>Nine</td></tr>
    <tr><td>Ten</td><td>Eleven</td><td>Twelve</td></tr>
</table>

下面是我的 Mootools 代码,

 $$('TABLE TBODY TR TD').addEvent('click',function(el)
                             {
                                 alert($(this).index());
                             });

看来代码是错误的,请有人让我知道获取元素属性的任何函数。

I am new to mootools but have good knowledge on jQuery,
I am trying to get the index of the on click.
Here is my HTML code..

<table>
    <tr><td>One</td><td>Two</td><td>Three</td></tr>
    <tr><td>Four</td><td>Five</td><td>Six</td></tr>
    <tr><td>Seven</td><td>Eight</td><td>Nine</td></tr>
    <tr><td>Ten</td><td>Eleven</td><td>Twelve</td></tr>
</table>

And below is my Mootools code,

 $('TABLE TBODY TR TD').addEvent('click',function(el)
                             {
                                 alert($(this).index());
                             });

It seems the code is wrong, Please someone let me know nay functions to get the properties of elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-12-04 02:58:40

首先,回调中的 $(this) 是 jQuery 的东西。在mootools中,要获取当前行的索引,您可以检索行的集合< /a>,迭代它们,并将点击事件附加到所有 td 子级:

$('table tr').each(function(row, index){ //row is the current tr, index is fairly self-explanatory :P

    //for each row, get its td children, attach the click event, and alert the 'index' (the number of the row)

    row.getElements('td').addEvent('click',function(){
        alert(index);
    });

    //or, if you just want to know the index of the row without doing something with each td, just attach the click event to the row        

});

演示: http://jsfiddle.net/steweb/UN5jd/

编辑:到也获取 td 索引,你可以这样做:

$('table tr').each(function(row, index){
    row.getElements('td').each(function(column, i){
        column.addEvent('click',function(){
            alert("Row: " + index + " --- " + "Column: "+i);
        });
    });
});

demo2: http://jsfiddle.net/steweb/UN5jd/2/

First of all, $(this) inside the callback is jQuery stuff. In mootools, to get the index of the current row you could retrieve the collection of rows, iterate them, and attach click event to all td children:

$('table tr').each(function(row, index){ //row is the current tr, index is fairly self-explanatory :P

    //for each row, get its td children, attach the click event, and alert the 'index' (the number of the row)

    row.getElements('td').addEvent('click',function(){
        alert(index);
    });

    //or, if you just want to know the index of the row without doing something with each td, just attach the click event to the row        

});

demo: http://jsfiddle.net/steweb/UN5jd/

EDIT : to get td indexes too, you could do something like this:

$('table tr').each(function(row, index){
    row.getElements('td').each(function(column, i){
        column.addEvent('click',function(){
            alert("Row: " + index + " --- " + "Column: "+i);
        });
    });
});

demo2: http://jsfiddle.net/steweb/UN5jd/2/

慢慢从新开始 2024-12-04 02:58:40

这是一种更干净的方法:

$('table tr td').addEvent('click', function(e){
    var index = this.getParent('tr').getElements('td').indexOf(this);
    console.log(index);
});

工作示例: http://jsfiddle.net/kt9aN/

Here is a cleaner way:

$('table tr td').addEvent('click', function(e){
    var index = this.getParent('tr').getElements('td').indexOf(this);
    console.log(index);
});

Working example: http://jsfiddle.net/kt9aN/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文