【问题标题】:Search HTML Table with jQuery使用 jQuery 搜索 HTML 表格
【发布时间】:2011-03-03 12:45:00
【问题描述】:

我有一张表格,需要特定行中的特定列。

<tr></tr>
<tr>
    <td></td>
    <td></td>
    <td class="important_column"><a href="/bla/blah/link">IMPORTANT INFO</a></td>
    <td></td>
    <td class="need_link_here"><a href="/I/WANT/THIS/LINK/">link</a></td>
</tr>
<tr></tr>

因此,如果“important_column”中的链接文本等于我要查找的内容。
获取“need link_here”列中的链接文本。 &lt;a&gt;&lt;/a&gt;之间的文字
在 jQuery 中怎么做?

【问题讨论】:

  • 部分解决方案:可以用这种方法搜索链接中的文字:stackoverflow.com/questions/926580/…
  • 我认为“链接文本”这个词在这里会造成一些混乱。您是指 中的 href 属性还是文本?
  • 对不起,如果我混淆了,我的意思是 里面的文字

标签: javascript jquery


【解决方案1】:

可以解决问题的 js 片段:

if($('.important_column a').text() == 'SOME TEXT YOU WANT')
{
   $('.important_column a').attr('href', $('.need_link_here a').attr('href'));
}

【讨论】:

    【解决方案2】:

    您可以使用:contains() 来检查元素是否包含特定文本:

    $("table td:contains('IMPORTANT INFO')")
        .next().next()
        .children("a");
    

    示例 - http://jsfiddle.net/Kkywt/

    【讨论】:

    • +1,因为这是迄今为止唯一不使用我怀疑在实际情况中会出现的 important_column 和 need_link_here 类的答案。
    • 该死,我弄错了,important_column 被使用了。
    • @snowlord:是的,但不是必须的。没有它,这里的代码也能正常工作。事实上,我只是编辑了答案和示例,以便您觉得您的 +1 是合理的;-)
    【解决方案3】:
    jQuery('.need_link_here a',
        jQuery('.important_column a:contains(IMPORTANT INFO)').parent() 
    ).attr('href');
    

    【讨论】:

    • OP 写道“如果“important_column”中的链接文本等于我要查找的内容,但您只需查看课程。
    • @snowlord:很好。已更正。
    • @jAndy:嗯,这是一个非常模糊的批评(这并不表明 为什么 它是“可怕的”或可以采取哪些措施来改进它)。
    【解决方案4】:

    目前还不清楚 OP 想要什么。这么近了吗?

    $('#myTable tr').each(function ()
    {
        var $tr = $(this);
        if($tr.find('td.important_column').text() === 'IMPORTANT INFO')
        {
            alert($tr.find('td.need_link_here > a').attr('href'))
        }
    });
    

    【讨论】:

      【解决方案5】:

      这样的?

      $(function() {
        column_text = $(".important_column a").text();
        if(column_text == "IMPORTANT INFO") {
          link_href = $(".need_link_here a").attr("href");
          alert(link_href);
        }
      });
      

      查看this working demo。

      【讨论】:

        【解决方案6】:

        您可以使用下一个方法来获取找到的项目的兄弟:

        $('.important_column').next().next().attr("href")
        

        将选择 class="important_column" 之后的第二个 td 元素的 href

        【讨论】:

        • 1.假设类不重要。 2. 不检查文本。 3.尝试获取TD元素的href属性值。
        • @David Dorward:“need_link_here”不会出现在实际页面中不是很明显吗?如果是这样,那将是微不足道的……
        • @snowlord:不,这并不明显,这个问题有很多解释。甚至不清楚“important_column”类是否存在。
        【解决方案7】:
        if ($("table tr td.important_column a[href=/bla/blah/link]").length) {
           var link = $("table tr td.need_link_here a").attr("href");
        }
        

        变化

        $("td.need_link_here a", $("table tr td.important_column a[href=/bla/blah/link]").closest(tr)).attr("href");
        

        【讨论】:

          猜你喜欢
          • 2020-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-07
          • 1970-01-01
          • 2016-05-01
          • 2011-11-01
          • 2018-03-28
          相关资源
          最近更新 更多