我应该如何访问<tr>在jquery中
How should I access the <tr>in jquery?
我有以下代码片段:
<table border="0">
<tr id="24655">
<td>
<a href="http://localhost/eprime/entprm/web/control/modules/questions/match_question.php?op=get_question_detail&question_ids=72224,24655,69412#searchPopContent" title="View question" class="inline_view_question_detail">QUE24655</a>
<a class="c-icn c-remove delete_question" onClick="return ConfirmDelete()" title="Delete question"> Delete</a><br />
</td>
<td class="question" align="center" valign="top">
92.86<br />
</td>
</tr>
</table>
现在你能告诉我在点击上面代码中<a>
标签中给出的超链接后,我应该如何访问jQuery中的<tr id="24655">
吗?
删除内联事件处理程序以支持jQuery one
<table border="0">
<tr id="24655">
<td>
<a href="http://localhost/eprime/entprm/web/control/modules/questions/match_question.php?op=get_question_detail&question_ids=72224,24655,69412#searchPopContent" title="View question" class="inline_view_question_detail">QUE24655</a>
<a class="c-icn c-remove delete_question" title="Delete question"> Delete</a>
<br />
</td>
<td class="question" align="center" valign="top">92.86
<br />
</td>
</tr>
</table>
然后在事件处理程序中,this
引用单击的元素,因此可以使用.closest()查找tr
元素,然后使用.attr()获取id属性
jQuery(function () {
$('.delete_question').click(function (e) {
var $this = $(this),
id = $this.closest('tr').attr('id');
alert(id);
e.preventDefault();
})
})
演示:Fiddle
try closest
var i = $(this).closest('tr').attr('id');
console.log(i);
在onclick函数调用中添加this
,然后收集对象并将其转换为jquery对象,但我建议您遵循Arun p Johny的答案,因为在这种情况下,您不必更改标记中的任何代码。
<table border="0">
<tr id="24655">
<td>
<a href="http://localhost/eprime/entprm/web/control/modules/questions/match_question.php?op=get_question_detail&question_ids=72224,24655,69412#searchPopContent" title="View question" class="inline_view_question_detail">QUE24655</a>
<a class="c-icn c-remove delete_question" onClick="return ConfirmDelete(this)" title="Delete question"> Delete</a><br />
</td>
<td class="question" align="center" valign="top">
92.86<br />
</td>
</tr>
</table>
<script type="text/javascript">
function ConfirmDelete(obj) {
console.debug($( this ).parent('tr[id]'));
}
</script>
<table border="0">
<tr id="24655">
<td>
<a href="http://localhost/eprime/entprm/web/control/modules/questions/match_question.php?op=get_question_detail&question_ids=72224,24655,69412#searchPopContent" title="View question" class="inline_view_question_detail">QUE24655</a>
<a class="c-icn c-remove delete_question" onClick="return ConfirmDelete()" title="Delete question"> Delete</a><br />
</td>
<td class="question" align="center" valign="top">
92.86<br />
</td>
</tr>
</table>
<script>
$(function(){
$('a.c-remove').click(function(){
var tr = $(this).parent().parent();
alert(tr.attr('id'));
});
});
</script>
相关文章:
- 使用Javascript,如何显示<tr>在另一<tr>悬停时,隐藏第二个<tr>
- 使用Razor/C#循环浏览HTML表以获得每个<tr>
- react-让一个元素返回两个相邻的<tr>标签
- jQuery只隐藏<tr>在一张桌子上
- 尝试应用于<tr>在ng单击中
- 使用'从HTML表中提取行的内容<tr onclick=alertContents()>'
- 如何将类添加到<tr>标记
- jQuery将输入集中在下一个tr->td时,td包含一个输入字段,但从不关注html选择
- <TR>标记未订阅事件
- 如何迭代TR>img
- jquery如何删除<tr>标记
- jQuery在<TR>TR折叠时标签不起作用
- 访问特定<td>对于给定<tr>以及更改html
- 有没有任何方法可以将json对象存储在HTML标记的默认/自定义属性中,比如<tr>
- 我应该如何访问<tr>在jquery中
- javascript页面加载检查是否存在具有特定类的span(在<tr>下),如果不存在,则删除整个<
- 删除当前<tr>并且还删除<tr>在使用纯javascript之前
- 使用js:order<创建表时出现问题;tr>并且<td>
- TinyMCE-允许文本作为<表>和/或<tr>
- ng重复不使用表<tr>但是与列表<李>