如何在以下代码中获取局部变量"head"的值?


function call(){
  var th;
  var head;
  $('#coToolTable').on('click', '.edit',function () {
    th = $('#coToolTable th').eq($(this).index());
    head=th.text();
    alert("head="+head);
  });
  var values1 = $('table tr td :checkbox:checked').map(function () {
    return $(this).closest('tr').find('td:first').text()
  }).get();
  alert("values="+values1+"head="+head);
  check(values1,head);
}

在此功能中,当我运行它时,alert("head="+head);给出了我的表标头的输出,但是在下一个警报框alert("values="+values1+"head="+head);中,它给出了head=undefined。为什么?

当您单击#coToolTable时,您将值分配给head,而不是在此之前。执行call()后,您的第二个警报将立即运行,这将是在单击事件处理程序运行并将值分配给head之前。因此,当第二个警报运行时,您会在声明时看到head的值: undefined

最新更新