如何从类名中读取值
我有下表:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解得很好,如果单击一个按钮,您需要获取该按钮位于哪一列,并确定该列的
thead
标题。我的小代码片段是这样做的:在
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:In
place
, the 0-based position of the button's parenttd
will be stored. This can be fetched using.index()
, which is executed on the set oftd
's in the correspondingtr
and gets the position of ourtd
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.
您可以使用
elem.className
,或者,如果您更喜欢使用 jQuery 方法,则可以使用$(elem).attr('className')
。在您的事件处理程序中,this
是elem
。但是,为了存储任意数据,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 theelem
.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')
尝试以下
Javascript
代码try the following
Javascript
code