Jquery.click href

Does anyone know why this is not working?

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

Pls help

+4
source share
1 answer

Wrap the click function in the document ready function, or place your code at the bottom of the page.

# main element does not exist when script is executed.

Example:

 <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> 
+14
source

Source: https://habr.com/ru/post/1332376/


All Articles