我应该如何访问<tr>在jquery中

How should I access the <tr>in jquery?

本文关键字:gt tr jquery lt 访问 我应该 何访问      更新时间:2024-04-25

我有以下代码片段:

<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>