how to get html table cell text value with JQuery?

BuZ*_*uZz 5 jquery

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]);
});
Run Code Online (Sandbox Code Playgroud)

The above selection snippet successfully displays :

[object HTMLTableCellElement] [object 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 ?

jQu*_*y00 6

Please use native code

ch[0].innerHTML
Run Code Online (Sandbox Code Playgroud)


Der*_*son 5

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

$(ch[0]).html()
Run Code Online (Sandbox Code Playgroud)

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

  • @jQuery00,是的,当然,它做了同样的事情,而且字符更少。但重点是“.html()”仅适用于 jQuery 对象。`ch` 和 `$(ch[0])` 都是 jQuery 对象;`ch[0]` 不是。 (2认同)