当用户点击名为winners的表时,我遇到了jquery片段的问题.代码
$('#winners tr').click(function() {
alert(223);
});
Run Code Online (Sandbox Code Playgroud)
只有在我在此代码段之前发出警报时才会触发:
alert(1);
$('#winners tr').click(function() {
alert(223);
});
Run Code Online (Sandbox Code Playgroud)
整个代码如下所示.问题是底部的代码片段.
<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>
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
问题是你的代码是在元素出现在DOM之前执行的,所以$('#winners tr')除非你有这个警告阻止脚本,否则它是空的.
通常的解决方案是将整个script元素放在元素的末尾body而不是元素的内部head.或者您可以将代码包装在jquery ready事件处理程序中.
当您使用谷歌加载时,最好的方法是将点击绑定代码移动到您传递给的回调中google.setOnLoadCallback.
| 归档时间: |
|
| 查看次数: |
332 次 |
| 最近记录: |