【问题标题】:Getting the text of the first <td> in the same row as another <td>获取与另一个 <td> 在同一行中的第一个 <td> 的文本
【发布时间】:2009-03-09 00:26:54
【问题描述】:

我遇到的问题越来越频繁,我有一个与&lt;td&gt; 绑定的事件处理程序,我需要获取该行中第一个&lt;td&gt; 中的文本(这是主键)。假设我不想添加任何额外的标记,那么最简单的方法是什么?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    使用 JQuery 你可以这样写:

    function eventHandler()
    {
        var firstTdText = $(this).parent().children("td:first").html();
    }
    

    当然,假设“this”引用了 html TD。

    【讨论】:

    • 谢谢。我应该测试过它。我在想 CSS 选择器。
    【解决方案2】:

    要引用该行中的第一个单元格,您可以使用 prevAll 选择器选择它:

    $(this).prevAll("td:last")
    

    从中获取文本:

    $(this).prevAll("td:last").html()
    // or,
    $(this).prevAll("td:last").text()
    

    【讨论】:

      【解决方案3】:

      你会去parent &lt;tr&gt;然后从父to the 1st child.

      【讨论】:

        猜你喜欢
        • 2017-12-19
        • 1970-01-01
        • 2019-01-06
        • 2021-05-04
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        相关资源
        最近更新 更多