Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery.click href_Jquery_Href - Fatal编程技术网

jquery.click href

jquery.click href,jquery,href,Jquery,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"&

有人知道为什么它不起作用吗

<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>

$('#main')。单击(函数(){
警报(“foobar”);
document.location.href='02.html';
});
正文{边距:0px;背景:#f2f2;}
#main{background:url(01.jpg)top-center不重复;高度:1745px;宽度:100%;文本-对齐:居中;溢出-x:隐藏;光标:指针;光标:手;}

请帮助

将单击功能包装在文档就绪功能中,或将代码放在页面底部

执行脚本时#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>

$(文档).ready(函数(){
$('#main')。单击(函数(){
document.location.href='02.html';
});
});

这是我不喜欢jQuery的一点——当您不选择任何对象时,它会返回一个有效但为空的对象。我知道这个功能很有用,但对初学者来说也很粗糙,而且很难调试。