Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取jQuery中表的行索引_Javascript_Jquery - Fatal编程技术网

Javascript 获取jQuery中表的行索引

Javascript 获取jQuery中表的行索引,javascript,jquery,Javascript,Jquery,我想在用户单击行时获取下表中元素的索引 <table class="table table-hover" id="event_table"> <thead> <tr> <th>Event Title</th> <th>Event Location</th> <th>Event Time</th> &

我想在用户单击行时获取下表中元素的索引

    <table class="table table-hover" id="event_table">
    <thead>
      <tr>
        <th>Event Title</th>
        <th>Event Location</th>
        <th>Event Time</th>
        <th>Event Date</th>
      </tr>
    </thead>
    <tbody>

        <tr>
          <td>Gathering</td>
          <td>City Centre</td>
          <td>10:30</td>
          <td>10/09/2016</td>
        </tr>
        <tr>
          <td>Meetup</td>
          <td>Some place</td>
          <td>12:30</td>
          <td>15/09/2016</td>
        </tr>


    </tbody>
  </table>

您的代码工作正常:

$event_table.onclick,tbody tr,function{ 警惕$this.index; }; 活动名称 活动地点 活动时间 活动日期 聚集 市中心 10:30 10/09/2016 会面 某处 12:30 15/09/2016
您的语法有点错误:$this.index。如果你想查看它,请调用alert或console.log。控制台中可能有错误?是的,我试图记录/提醒它,但似乎什么都没有发生。这很奇怪,我花了一个小时调试,但它无法工作。表正在动态加载,可能与此有关。我去检查。是的,所以我想出来了,因为它是动态加载的,我需要在添加新行之后添加onclick,函数{…}。实际上,你不必关心序列。我已经更新了js代码片段。它应该也适用于ajax加载的TR。
$("#event_table tbody tr").on("click", function() {
   $(this).index(); 
});