查找使用jQuery知道行和列ID的表单元格 [英] Find table cell knowing row and column ids with jQuery

查看:65
本文介绍了查找使用jQuery知道行和列ID的表单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表,其中设置了标题的ID和行的ID.给定两者的ID,我需要在两者的交点处找到一个对应的单元格.

I have a simple table, where I have set IDs for headers and IDs for rows. Given an ID for both, I need to find a corresponding cell at the intersection of those two.

示例:

<table id="tablica">
    <thead>
        <th id="bla">Bla</th>
        <th id="bli">Bli</th>
        <th id="blu">Blu</th>
    </thead>
    <tbody>
        <tr id="jedan">
            <td>1</td>            
            <td>2</td>
            <td>3</td>
        </tr>
        <tr id="dva">
            <td>4</td>            
            <td>5</td>
            <td>6</td>
        </tr>
        <tr id="tri">
            <td>7</td>            
            <td>8</td>
            <td>9</td>
        </tr>
    </tbody>
</table>​​​

因此,如果我有id ="bli"和id ="dva",则意味着在此示例中,我想对值为5的单元格进行处理.

So if I have id="bli" and id="dva", that means I want to do something with cell having value 5 in this example.

编辑:有很多正确答案,我全都赞成,但不幸的是,我只能选择一个正确答案.

edit: There are a lot of correct answers, I upvoted all of them, but unfortunately I can only choose one as correct.

推荐答案

这是我的解决方案:

var column = $('#bli').index();
var cell = $('#dva').find('td').eq(column);

jsfiddle上的工作示例: http://jsfiddle.net/t8nWf/2/

And working example on jsfiddle: http://jsfiddle.net/t8nWf/2/

将所有内容添加到一个函数中:

added all in a function:

function getCell(column, row) {
    var column = $('#' + column).index();
    var row = $('#' + row)
    return row.find('td').eq(column);
}

工作示例: http://jsfiddle.net/t8nWf/5/

这篇关于查找使用jQuery知道行和列ID的表单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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