如果我知道HTML表单元格的行和列索引,如何更改该单元格中的属性?
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I know nothing about jQuery but am an experienced C++ programmer (not sure if that helps or hurts). I found jQuery code that gives me the row and column index of a cell in an HTML table when a user clicks on that cell. Using such row-column index numbers, I need to change an attribute's value in the previously selected cell and in the cell just clicked. The index numbers are produced and saved with this code:

var $trCurrent = 0; // Index of cell selected when page opens 
var $tdCurrent = 0; // i.e., previously selected cell

$(document).ready(function ()
{
    $("td").click(function ()
    {
        // How toclear previously selected cell's attribute here? ('class', 'recent')
        var oTr = $(this).parents("tr");
        $tdCurrent = oTr.children("td").index(this);

     });
    $("tr").click(function ()
    {
        $trCurrent = $(this)[0].rowIndex;
        // How to set new attributes here? ('class', 'current');
        // and continue work using information from currently selected cell

     });
});

Any help or hints would be appreciated. I do not even know if this is the way I should get the index of the row and column.

#0

I would do this slightly different, if I understand your requirements. If when you click on a cell you need to do something to the previous clicked cell, use a class. So:

$("td").click(function() {
  $("td.active").removeClass("active");
  $(this).addClass("active");
});

So basically each time you click a cell, the previous active cell has its class removed and the new cell has it added. In the above code where I remove the class, you can do anything else you like to it and this avoids the problem of storing and referencing row/cell numbers.

If your goal is simply to style the cell differently, use that same class in your CSS eg:

td.active { background: yellow; }

When you render the page, you can make whichever cell you like active by giving it that class.

If you need to know the current and previous cells try this:

$("td").click(function() {
  $("td.active").removeClass("current").addClass("previous");
  $(this).addClass("current");
});

And then at any point you can do:

$("td.current")...
$("td.previous")...

If you really do need to know the row/cell number clicked try:

var rownum;
var cellnum;
$("td").click(function() {
  var row = $(this).closest("tr");
  rownum = $("tr").index(row);
  cellnum = row.children("td").index(this);
});

And if you need to reference that at any point:

$("tr:eq(" + rownum + ") > td:eq(" + cellnum + ")")...

推荐文章

Zend窗体和嵌套字段集/容器?

Zend窗体和嵌套字段集/容器?

推荐文章

如何链接到另一页的选项卡部分?

如何链接到另一页的选项卡部分?

推荐文章

转移到修改过的网站;如何避免seo下降;文件路径的使用

转移到修改过的网站;如何避免seo下降;文件路径的使用

推荐文章

内存中推送参数的配置

内存中推送参数的配置

推荐文章

创建模型时出错asp.net MVC 2预览版和亚音速3.0.0.3

创建模型时出错asp.net MVC 2预览版和亚音速3.0.0.3

推荐文章

prototype-找到下一个兄弟的优雅代码

prototype-找到下一个兄弟的优雅代码

推荐文章

强制浏览器重新读取缓存的图像

强制浏览器重新读取缓存的图像

推荐文章

把人分成1:1或2:2小组的算法

把人分成1:1或2:2小组的算法

推荐文章

依赖性=>销毁“has_many through”关联

依赖性=>销毁“has_many through”关联

推荐文章

新的字符串类型在Delphi2009/2010中是如何工作的?

新的字符串类型在Delphi2009/2010中是如何工作的?

推荐文章

使用Tibco本地Libs的Java Webstart

使用Tibco本地Libs的Java Webstart

推荐文章

如何从java打开IE,通过java执行click()等操作?

如何从java打开IE,通过java执行click()等操作?

推荐文章

自定义生成任务:如何将输出写入生成日志文件

自定义生成任务:如何将输出写入生成日志文件

推荐文章

如果文本框不是任何表单的一部分,如何在javascript中引用html页面正文中的文本框?

如果文本框不是任何表单的一部分,如何在javascript中引用html页面正文中的文本框?

推荐文章

Delphi中的静态多态性

Delphi中的静态多态性

推荐文章

简单的Java声音小程序不起作用!!! 啊!

简单的Java声音小程序不起作用!!! 啊!