<html> <head> <scripttype="text/javascript"src="jquery.js"></script> <scripttype="text/javascript"> $(document).ready(function(){ $("p").click(function(){ $(this).hide(); }); }); </script> </head> <body> <p>If you click on me, I will disappear.</p> </body> </html>
如果把$(document).ready(function() {});去掉后,无法隐藏段落:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<html> <head> <scripttype="text/javascript"src="jquery.js"></script> <scripttype="text/javascript"> $("p").click(function(){ $(this).hide(); }); </script> </head> <body> <p>If you click on me, I will disappear.</p> </body> </html>
但是把script放到页面最后的话,就可恢复隐藏效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<html> <head> </head> <body> <p>If you click on me, I will disappear.</p> </body> <scripttype="text/javascript"src="jquery-1.7.2.min.js"></script> <scripttype="text/javascript"> $("p").click(function(){ $(this).hide(); }); </script> </html>