$(document).ready() 里的代码是在页面内容都加载完才执行的,如果把代码直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,此时如果你标签里执行的代码调用了当前还没加载过来的代码或者dom,那么就会报错,当然如果你把script标签放到页面最后面那么就没问题了,此时和ready效果一样。
$(document).ready(function(){})可以简写成$(function(){});
点击段落后,此段落隐藏:
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/xiazai/code/8606">
<img src="https://img.php.cn/upload/webcode/000/000/015/175023720219333.jpg" alt="佳蓝智能应答系统">
</a>
<div class="aritcle_card_info">
<a href="/xiazai/code/8606">佳蓝智能应答系统</a>
<p>类似智能机器人程序,以聊天对话框的界面显示,通过输入问题、或点击交谈记录中的超链接进行查询,从而获取访客需要了解的资料等信息。系统自动保留用户访问信息及操作记录。后台有详细的设置和查询模块。适用领域:无人职守的客服系统自助问答系统智能机器人开发文档、资源管理系统……基本功能:设置对话界面的显示参数设置各类展示广告根据来访次数显示不同的欢迎词整合其他程序。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="佳蓝智能应答系统">
<span>4</span>
</div>
</div>
<a href="/xiazai/code/8606" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="佳蓝智能应答系统">
</a>
</div>
</body>
</html>
如果把$(document).ready(function() {});去掉后,无法隐藏段落:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$("p").click(function(){
$(this).hide();
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
但是把script放到页面最后的话,就可恢复隐藏效果:
<html>
<head>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$("p").click(function(){
$(this).hide();
});
</script>
</html>
总结:
$(document).ready 里的代码是在页面内容都加载完才执行的,你直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,如果你标签里执行的代码调用了当前还没加载过来的代码或者dom,那么就会报错,
当然如果你把script标签当到页面最后面那么就没问题了和ready差不多的效果
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号