Javascript jQuery:如何将表属性动态添加到行中的特定单元格?

Javascript jQuery:如何将表属性动态添加到行中的特定单元格?,javascript,jquery,Javascript,Jquery,我有一个通过JavaScript和jQuery生成的表。在一些单元格中有一个目录字符串,catalogIndex,它通过CSS被截断。我希望能够将title属性添加到那些特定的单元格中,以便用户可以看到整个字符串 我曾尝试在jQuery中使用.attr方法,如下所示: //variable that uses queueRow and creates a new cell. var catCell = queueRow.insertCell(); //add text

我有一个通过JavaScript和jQuery生成的表。在一些单元格中有一个目录字符串,
catalogIndex
,它通过CSS被截断。我希望能够将title属性添加到那些特定的单元格中,以便用户可以看到整个字符串

我曾尝试在jQuery中使用
.attr
方法,如下所示:

    //variable that uses queueRow and creates a new cell.
    var catCell = queueRow.insertCell();

    //add text to the cell using the content of catalogIndex
    catCell.innerHTML = catalogIndex;
$(queueRow[5]).attr('title',catalogIndex)

queueRow
是一个变量,用于保存HTML表的实际行。在前面的代码中,我使用以下方法创建了它:

var queueRow=document.getElementById(“copyQueue”).insertRow()

我可以像这样插入单个单元格:

    //variable that uses queueRow and creates a new cell.
    var catCell = queueRow.insertCell();

    //add text to the cell using the content of catalogIndex
    catCell.innerHTML = catalogIndex;

我试图以适当的单元格为目标(在我的例子中,是
queueRow
的第6个位置)并添加title属性。我没有收到任何错误,但该单元格中似乎没有添加任何内容。获取所需单元格位置的正确语法是什么?

您可以使用
单元格
属性:

var queueRow = document.getElementById("copyQueue").insertRow();
// ...
var catCell = queueRow.insertCell();
catCell.textContent = catalogIndex; // don't use innerHTML for pure text.
// ...
$(queueRow.cells[5]).attr('title', catalogIndex);
但是您应该尝试更多地使用jQuery。例如:

var $queueRow = $("<tr>").appendTo("#copyQueue");
// ...
var $catCell = $("<td>").appendTo($queueRow).text(catalogIndex);
// ...
$("td", $queueRow).eq(5).attr('title', catalogIndex);

如何获取
queueRow
对象?您是否尝试过
console.log(queueRow[5])
以确保这是正确的DOM对象?请添加表和代码的示例。而不是
queueRow[5]
do
queueRow.cells(5)
。但实际上,当您使用jQuery时,为什么要使用这些长的
document.getElementById
,…等等,而jQuery语法却如此简洁?@trincot,我得到一个错误,说
queueRow.cells
不是一个函数。看起来
.cells()
是我不使用的插件的一部分。有其他选择吗?很抱歉,应该是
queueRow.cells[5]
这是表行元素的标准DOM属性。您看到的另一个Javascript是遗留代码,所以不用担心,它可能很快就会被转换!谢谢你的帮助。