jquery根据id匹配元素

发布于 2024-12-26 05:08:44 字数 2237 浏览 4 评论 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()
      ]

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

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

I'm a noob to jquery. Stuck and would appreciate some help.
Currently building a tool to grab data from a particular page. The page looks like this:

<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>

Below is a snippet of the 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()
      ]

I'm trying to capture all data from just the <tr> blocks with id parent0, and child0.

In it's current working state, the tool captures all data within the two tables as text. Ideally I'd like to be able to capture all the <TR> blocks separately, put them in an array that I can then iterate over.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯狂的代价 2025-01-02 05:08:44

看起来您正在使用 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();
                          }
                         )

It looks like you are using ID elements where you should be using a class.

Instead of

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

Do this

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

Unlike ID, you can assign the same value to multiple elements with the class attribute.

Then you can select them all like this

$("tr.child0,tr.parent0").each(
                          function() {
                             //this will be fired for each matched element
                             $(this).doSomething();
                          }
                         )
唠甜嗑 2025-01-02 05:08:44
$('tr[id="child0"]').each(function() {
                         //this will be fired for each matched element
                         $(this).doSomething();
                      }
                     );

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

$('tr[id="child0"]').each(function() {
                         //this will be fired for each matched element
                         $(this).doSomething();
                      }
                     );

this way you will get all tr whose id is child0
check this link for more reference http://api.jquery.com/category/selectors/

七色彩虹 2025-01-02 05:08:44

这样做...

$('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');;

DO like that...

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

Edit: though that will match "blablablabla" as well.

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

That should be more accurate.

Edit: Actually, I think this solution is more elegant:

$('cite.fn').filter(function () {
    return $(this).text() == 'blabla';
}).css('color', 'red');;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文