如何从类名中读取值

发布于 2024-11-01 19:57:08 字数 2798 浏览 1 评论 0原文

我有下表:

<table>
    <thead> 
        <tr>
            <th class="tTle">Mon</td>
            <th class="tTle">Tues</td>
            <th class="tTle">Wed</td>
        <tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="monday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="tuesday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="wednesday"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="monday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="tuesday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="wednesday"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="monday"/>
            </td>>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="tuesday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="wednesday"/>
            </td>
        </tr>
    </tbody>
</table>

当我单击 but 时,我想读取 tTle 各自的 thead 值?我怎样才能做到这一点?

$(function ()
    {
        
         $(".but").click(readTitle);      
            
  
        });
});

I have the following table:

<table>
    <thead> 
        <tr>
            <th class="tTle">Mon</td>
            <th class="tTle">Tues</td>
            <th class="tTle">Wed</td>
        <tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="monday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="tuesday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="wednesday"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="monday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="tuesday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="wednesday"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="monday"/>
            </td>>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="tuesday"/>
            </td>
            <td>
                <input type="text" class = "Val1"/>
                <input type="text" class = "Val2"/>
                <input type="button" class = "but" val="wednesday"/>
            </td>
        </tr>
    </tbody>
</table>

I want to read the respective thead value for tTle when I click on but? How can I do that?

$(function ()
    {
        
         $(".but").click(readTitle);      
            
  
        });
});

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

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

发布评论

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

评论(3

烟花易冷人易散 2024-11-08 19:57:08

如果我理解得很好,如果单击一个按钮,您需要获取该按钮位于哪一列,并确定该列的 thead 标题。我的小代码片段是这样做的:

$('.but').click(function () {
  var $me=$(this);
  var place=$('td', $me.closest('tr')).index($me.closest('td'));

  var text=$('thead .tTle').eq(place).text();
});

place 中,将存储按钮父级 td 从 0 开始的位置。这可以使用 .index() 获取,该方法在相应 tr 中的 td 集合,并获取其中 td 的位置。

当我们有了索引后,很容易使用 找到我们正在寻找的 .tTle .eq()

编辑:以及承诺的jsFiddle。它似乎工作得很好,尽管OP在这个答案下面的评论中指出它必须改变。可能是一些标记问题。

If I understood well, if a button is clicked, you want to get which column is this button in, and the determine the thead title for that column. My little snippet does that:

$('.but').click(function () {
  var $me=$(this);
  var place=$('td', $me.closest('tr')).index($me.closest('td'));

  var text=$('thead .tTle').eq(place).text();
});

In place, the 0-based position of the button's parent td will be stored. This can be fetched using .index(), which is executed on the set of td's in the corresponding tr and gets the position of our td inside that.

When we have the index, it is easy to find the .tTle we are looking for using .eq().

Edit: And the promised jsFiddle. It seems to work fine, though OP indicated in the comments below this answer that it has to be changed. Might be some markup problems.

递刀给你 2024-11-08 19:57:08

您可以使用 elem.className,或者,如果您更喜欢使用 jQuery 方法,则可以使用 $(elem).attr('className')。在您的事件处理程序中,thiselem

但是,为了存储任意数据,HTML5 数据属性(无需实际设置 HTML5 文档类型也能正常工作)更好:

和 <代码>$('...').data('某事')

You can use elem.className or, if you prefer using a jQuery method, $(elem).attr('className'). In your event handler, this is the elem.

However, to store arbitrary data, the HTML5 data attributes (also work fine without actually setting a HTML5 doctype) are better:

<sometag data-something="text-or-json"> and $('...').data('something')

老娘不死你永远是小三 2024-11-08 19:57:08

尝试以下 Javascript 代码

$(function ()
                {

                    $(".but").click(function(){

                        alert("here");
                        var nTd = this.parentNode;
                        alert(nTd.getElementsByClassName("Val1")[0].value)
                        alert(nTd.getElementsByClassName("Val2")[0].value)
                    });


                });

try the following Javascript code

$(function ()
                {

                    $(".but").click(function(){

                        alert("here");
                        var nTd = this.parentNode;
                        alert(nTd.getElementsByClassName("Val1")[0].value)
                        alert(nTd.getElementsByClassName("Val2")[0].value)
                    });


                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文