2009-09-24 75 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); 
} 
相關問題