2010-12-16 58 views
4

有人知道它为什么不起作用?jquery .click href

<script src="http://code.jquery.com/jquery-1.4.4.js"></script> 
<script> 
    $('#main').click(function() { 
    alert('foobar'); 
    document.location.href='02.html'; 
    }); 

</script> 
<style type="text/css"> 
    body {margin:0px; background:#f2f2f2;} 
    #main {background:url(01.jpg) top center no-repeat; height:1745px; width:100%; text- align:center; overflow-x:hidden; cursor:pointer; cursor:hand;} 
</style> 
</head> 
<body> 
    <div id="main"></div> 
</body> 

请帮助

回答

14

裹点击功能在文档准备功能或把你的代码放在网页的底部。

执行脚本时#main元素不存在。

例如:

<script src="http://code.jquery.com/jquery-1.4.4.js"></script> 
<script> 
$(document).ready(function() { 
    $('#main').click(function() { 

    document.location.href='02.html'; 
    }); 
}); 
</script> 
+1

这是一两件事,我不喜欢jQuery的 - 当您选择任何对象,它返回一个有效的,但空对象。我知道这个功能很有用,但对初学者来说也很粗糙,而且很难调试。 – 2010-12-16 16:51:52