2009-09-24 78 views
1

我正在学习如何创建插件,并在如何创建自己的自定义选择器时遇到问题。如何在jquery插件中返回自定义/创建的选择器

如果我有以下

<table id="myTable"> 
    <tr><td></td>........<td></td></tr> 
    . 
    . 
    . 
    <tr><td></td>........<td></td></tr> 
</table> 

与第n行n列的表,我想创建一个具有指向到指定的行和列

这是怎么选择一个插件插件功能看起来像

$.fn.Cell = function(row,col){ 
    //select the cell here ... assuming the target element is a table above 
    // this could somehow written below 
    var mycell = $(this).children().find('tr:eq(' + row + ')').children().find('td:eq(' + col + ')'); 
    // return the selector here 

}; 

然后,我应该在应用程序代码是这样的:

$("#myTable").Cell(2,3).text("Wow"); // this writes a text to row 2, col 3. 

你能帮忙填写缺失的代码吗?我试图寻找可用的插件,但从来没有找到像这样的功能。我更喜欢知道它如何工作,而不是知道现有插件的名称和链接。我的目标是学习制作插件的过程,并掌握jQuery和JavaScript。

回答

1

试试这个:

$.fn.Cell = function(row, col){ 
    return $('tr:nth-child('+row+')>td:nth-child('+col+')', this); 
} 
相关问题