开发者

jQuery DataTables: How to get row index (or nNode) by row id of tr?

开发者 https://www.devze.com 2023-04-06 06:20 出处:网络
I have a dataTables <table id=\"myTable\">. I would like to fnUpdate() and fnDestroy() my rows. every row has an id, eg: <tr id=\"16\">.

I have a dataTables <table id="myTable">. I would like to fnUpdate() and fnDestroy() my rows. every row has an id, eg: <tr id="16">. To fnUpdate()/fnDestroy() the appropriate <tr>, I need to get that row's index. For this I try to use fnGetPosition(), but the way I try it is not the way to do it:

$("#myTable").fnGetPosition( $("#16") )

results in

TypeError: nNode.nodeName is undefined [Break On This Error] var sNodeName = nNode.nodeName.toUpperCase();

Which makes sense, as fnGetPosition() expexts nNode (in my case a HTMLTableRowElement).

How do I get the HTMLTableRowElement that has id="16" ?

EDIT: A correct answer to my question is: document.getElementById("16"). Based on that, I would like to change my question to:

Why does

$("#myTable").fnGetPosition( document.getElementById("16") ) 

work, but

$("#myTable").fnGetPosition( $("#16") )
开发者_Go百科

fails?


For anyone who still has this problem, try this:

$("#myTable").fnGetPosition( $("#16")[0] )

To get the same result as document.getElementById you should access the first element in the jQuery object.


document.getElementById() returns a DOM object, and everything on the DOM object will be inherently accessible.

JQuery's $('#...') returns a wrapper around a single DOM object OR a set of DOM objects (depending on the selector) and as such, it does not return the actual DOM Object. It makes it easier to work with DOM objects.

The reason you are getting that error in the second case would be that $(#...) is not actually a DOM object.


You sould do:

var oTable = $('#myTable').dataTable();
oTable.fnGetPosition( $("#myTable #16") );
0

精彩评论

暂无评论...
验证码 换一张
取 消