为什么当鼠标点击表格行时我无法获得行ID?

Lee*_*eem 1 html javascript jquery jquery-ui javascript-events

我有一个带有表格的index.html页面:

<body>
      <table class="mytable">
          <tr id="1">
              <td>John</td>
              <td>123</td>
          </tr>
           <tr id="2">  
              <td>Kate</td>
              <td>456</td>
          </tr>
      </table>

  <script src="my.js"></script>
  </body>
Run Code Online (Sandbox Code Playgroud)

我已将my.js包含在我的页面中,我试图在my.js中以下列方式获取鼠标单击行的id:

$('.mytable tr').click(function(e){
                   alert('clicked.'); //even did not get alert here.
                   var rowId = $(this).attr('id');
               alert(rowId);
        });
Run Code Online (Sandbox Code Playgroud)

但我没有得到点击行的行ID(没有警报),我在哪里错了?

- -解 - -

我的坏,我有一个地方清空了桌子,这意味着没有tr.这就是为什么它不起作用.很抱歉打扰.

Mat*_*all 5

  1. $ === jQuery
  2. 绑定点击侦听器的代码是否在文档就绪处理程序中执行?
  3. this.id 已经足够,可以在所有浏览器中使用.