0

我是 jquery 的菜鸟。卡住了,希望能得到一些帮助。目前正在构建一个从特定页面获取数据的工具。页面如下所示:

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
 <tbody>  //This is data group 1    
  <tr id="parent0">   //Record 1
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
  <tr id="parent0">   //Record 2
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
 </tbody>
</table>

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
 <tbody>  //This is data group 2    
  <tr id="child0">   //Record 1
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
  <tr id="child0">   //Record 2
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
 </tbody>
</table>

下面是jquery的一个片段:

ancestor = $(this).closest("tr[id]");

  matchedElement = $(this).first();
  originalBgColor = $(matchedElement).css('background-color');
  $(matchedElement).css('background-color', 'green');
  $(matchedElement).bind('click.annotator', function(event) {
    event.stopPropagation();
    event.preventDefault();
    self.port.emit('show',
      [
        document.location.toString(),
        $(ancestor).attr("child0"),
        $(matchedElement).text()
      ]

我正在尝试仅从<tr>ID 为 parent0 和 child0 的块中捕获所有数据。

在其当前工作状态下,该工具将两个表中的所有数据作为文本捕获。理想情况下,我希望能够分别捕获所有<TR>块,将它们放在一个数组中,然后我可以对其进行迭代。

4

3 回答 3

2

看起来您在应该使用类的地方使用 ID 元素。

代替

<tr id="child0">
<tr id="parent0">

做这个

<tr class="child0">
<tr class="parent0">

ID不同,您可以使用 class 属性为多个元素分配相同的值。

然后你可以像这样选择它们

$("tr.child0,tr.parent0").each(
                          function() {
                             //this will be fired for each matched element
                             $(this).doSomething();
                          }
                         )
于 2012-01-11T01:08:41.130 回答
0
$('tr[id="child0"]').each(function() {
                         //this will be fired for each matched element
                         $(this).doSomething();
                      }
                     );

这样,您将获得 id 为 child0 的所有 tr 检查此链接以获取更多参考http://api.jquery.com/category/selectors/

于 2012-01-11T13:27:48.793 回答
0

那样做...

$('cite.fn:contains(blabla)').css('color', 'red');

编辑:虽然这也会匹配“blablablabla”。

$('cite.fn').each(function () {
    if ($(this).text() == 'blabla') {
        $(this).css('color', 'red');
    }
});

那应该更准确。

编辑:实际上,我认为这个解决方案更优雅:

$('cite.fn').filter(function () {
    return $(this).text() == 'blabla';
}).css('color', 'red');;
于 2016-04-22T11:31:40.613 回答