【问题标题】:How to return a customized/created selector in jquery plugin如何在 jquery 插件中返回自定义/创建的选择器
【发布时间】:2009-09-24 13:27:01
【问题描述】:

我正在学习如何创建插件,但在如何创建自己的自定义选择器时遇到问题。

如果我有一张下面有第 n 行第 n 列的表格

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

我想创建一个插件,它有一个指向指定行和列的选择器

这可能是插件功能的样子

$.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。

【问题讨论】:

    标签: jquery jquery-plugins selector


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2012-08-25
      相关资源
      最近更新 更多