如何使用JQuery获取html表格单元格文本值?

时间:2022-06-19 22:25:29

I manage to get a collection of cells in a row when this row is clicked with the following:

当单击这一行时,我设法将单元格集合放在一行中:

$('.NamesGridClass tbody tr').bind('click', function() {
    var ch = $(this).children();
    alert(ch[0] + ' ' + ch[1]);
});

The above selection snippet successfully displays :

以上选择片段成功显示:

[object HTMLTableCellElement] [object HTMLTableCellElement]

(对象HTMLTableCellElement][对象HTMLTableCellElement]

I have tried ch[0].html(), ch[0].val(), ch[0].text(), and get errors. How can I get the content of my cells here ?

我试过ch[0].html()、ch[0].val()、ch[0].text(),都有错误。如何获取单元格的内容?

3 个解决方案

#1


4  

In order to use .html(), you need to turn those objects into jQuery objects:

要使用.html(),需要将这些对象转换为jQuery对象:

$(ch[0]).html()

Same would apply to any other valid jQuery method you wish to apply.

同样适用于您希望应用的任何其他有效jQuery方法。

#2


6  

Please use native code

请使用本机代码

ch[0].innerHTML

#3


2  

When you access an item in an array you get from jQuery, you get back plain old javascript elements, not jquery elements. You can either use the javascript .innerHTML or double wrap the the value with the jquery selector. I would recommend using .innerHTML since its simpler, so

当您访问从jQuery获得的数组中的项时,您将返回普通的旧javascript元素,而不是jQuery元素。您可以使用javascript . innerhtml或用jquery选择器将值加倍。我建议使用。innerhtml,因为它更简单

$('.NamesGridClass tbody tr').bind('click', function() {
        var ch = $(this).children();
        alert(ch[0].innerHTML + ' ' + ch[1].innerHTML);
    });

#1


4  

In order to use .html(), you need to turn those objects into jQuery objects:

要使用.html(),需要将这些对象转换为jQuery对象:

$(ch[0]).html()

Same would apply to any other valid jQuery method you wish to apply.

同样适用于您希望应用的任何其他有效jQuery方法。

#2


6  

Please use native code

请使用本机代码

ch[0].innerHTML

#3


2  

When you access an item in an array you get from jQuery, you get back plain old javascript elements, not jquery elements. You can either use the javascript .innerHTML or double wrap the the value with the jquery selector. I would recommend using .innerHTML since its simpler, so

当您访问从jQuery获得的数组中的项时,您将返回普通的旧javascript元素,而不是jQuery元素。您可以使用javascript . innerhtml或用jquery选择器将值加倍。我建议使用。innerhtml,因为它更简单

$('.NamesGridClass tbody tr').bind('click', function() {
        var ch = $(this).children();
        alert(ch[0].innerHTML + ' ' + ch[1].innerHTML);
    });