Javascript 必须在触发事件之前写入警报

Javascript 必须在触发事件之前写入警报,javascript,jquery,Javascript,Jquery,当用户单击一个名为winners的表时,jquery代码片段有一个问题。代码 $('#winners tr').click(function() { alert(223); }); 仅当我在以下代码段之前放置警报时才会激发: alert(1); $('#winners tr').click(function() { alert(223); }); 整个代码如下所示。问题是底部的代码片段 <script type="text/javascri

当用户单击一个名为winners的表时,jquery代码片段有一个问题。代码

 $('#winners tr').click(function() {
      alert(223);
    });
仅当我在以下代码段之前放置警报时才会激发:

  alert(1);
  $('#winners tr').click(function() {
    alert(223);
   });
整个代码如下所示。问题是底部的代码片段

 <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript">
  google.load("jquery", "1.7.1");

  function listen(last_modified, etag) {
       $.ajax({
           'beforeSend': function(xhr) {
               xhr.setRequestHeader("If-None-Match", etag);
               xhr.setRequestHeader("If-Modified-Since", last_modified);
           },
           url: '/test/sub',
           dataType: 'text',
           type: 'get',
           cache: 'false',
           success: function(data, textStatus, xhr) {
                     /*REMOVED CODE*/
               });

               /* Start the next long poll. */
               listen(last_modified, etag);
           },
           error: function(xhr, textStatus, errorThrown)
           {
             //$('#data').prepend(textStatus + ' | ' + errorThrown);
           }
       }); 
   }; 

   google.setOnLoadCallback(function() {
       /* Start the first long poll. */
       /* setTimeout is required to let the browser know
          the page is finished loading. */
       setTimeout(function() {
           listen('Thu, 1 Jan 1970 00:00:00 GMT', '0');
       }, 500);
   });
    //alert(1);
    $('#winners tr').click(function() {
      alert(223);
    });
  </script>

load(“jquery”,“1.7.1”);
函数侦听(上次修改,etag){
$.ajax({
“beforeSend”:函数(xhr){
setRequestHeader(“如果不匹配”,etag);
xhr.setRequestHeader(“如果修改自”,最后一次修改);
},
url:“/test/sub”,
数据类型:“文本”,
键入:“get”,
缓存:“false”,
成功:函数(数据、文本状态、xhr){
/*删除的代码*/
});
/*开始下一个长投票*/
听(最后修改,etag);
},
错误:函数(xhr、textStatus、errorshown)
{
//$(“#数据”).prepend(textStatus+“|”+errorshown);
}
}); 
}; 
setOnLoadCallback(函数(){
/*开始第一次长轮询*/
/*需要设置超时才能让浏览器知道
页面加载完毕*/
setTimeout(函数(){
听('Thu,1970年1月1日00:00:00 GMT','0');
}, 500);
});
//警报(1);
$('#tr')。单击(函数(){
警报(223);
});

谢谢你的帮助

问题在于,您的代码是在元素出现在DOM中之前执行的,因此
$(“#winners tr')
为空,除非此警报阻止脚本

通常的解决方案是将整个
脚本
元素放在
主体
元素的末尾,而不是放在
头部
中。或者,您可以将代码包装在jquery就绪的事件处理程序中


当您使用google load时,最好将单击绑定代码移动到您传递给
google.setOnLoadCallback

的回调中,如果您的代码位于页面的头部,请将其放置在文档就绪处理程序中-

$(document).ready(function() {
  function listen(last_modified, etag) {
       $.ajax({
           'beforeSend': function(xhr) {
               xhr.setRequestHeader("If-None-Match", etag);
               xhr.setRequestHeader("If-Modified-Since", last_modified);
           },
           url: '/test/sub',
           dataType: 'text',
           type: 'get',
           cache: 'false',
           success: function(data, textStatus, xhr) {
                     /*REMOVED CODE*/
               });

               /* Start the next long poll. */
               listen(last_modified, etag);
           },
           error: function(xhr, textStatus, errorThrown)
           {
             //$('#data').prepend(textStatus + ' | ' + errorThrown);
           }
       }); 
   }; 

   google.setOnLoadCallback(function() {
       /* Start the first long poll. */
       /* setTimeout is required to let the browser know
          the page is finished loading. */
       setTimeout(function() {
           listen('Thu, 1 Jan 1970 00:00:00 GMT', '0');
       }, 500);
   });
    //alert(1);
    $('#winners tr').click(function() {
      alert(223);
    });
});

CTRF+F5
每次刷新页面时,都应该将事件侦听器放在document.ready函数中,以确保dom已准备就绪。使用
google.load(“jquery”,“1.7.1”),因此必须使用回调,read docs Document ready将无法工作,因为OP使用的是
google.load(“jquery”,“1.7.1”),因此必须使用回调