jQuery 使用注意事项 与 小技巧(tips)
jQuery 使用注意事项 与 小技巧(tips)
1
$( document ).ready()
https://learn.jquery.com/using-jquery-core/document-ready/
A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside
$( document ).ready()will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside$( window ).load(function() { ... })will run once the entire page (images or iframes), not just the DOM, is ready.
1234// A $( document ).ready() block.$( document ).ready(function() {console.log( "ready!" );});Experienced developers sometimes use the shorthand
$()for$( document ).ready(). If you are writing code that people who aren't experienced with jQuery may see, it's best to use the long form.
1234// Shorthand for $( document ).ready()$(function() {console.log( "ready!" );});You can also pass a named function to
$( document ).ready()instead of passing an anonymous function.
123456789// Passing a named function instead of an anonymous function.function readyFn( jQuery ) {// Code to run when the document is ready.}$( document ).ready( readyFn );// or:$( window ).load( readyFn );The example below shows
$( document ).ready()and$( window ).load()in action. The code tries to load a website URL in an<iframe>and checks for both events:
1234567891011121314151617<html><head><script src="https://code.jquery.com/jquery-1.9.1.min.js"></script><script>$( document ).ready(function() {console.log( "document loaded" );});$( window ).load(function() {console.log( "window loaded" );});</script></head><body><iframe src="http://techcrunch.com"></iframe></body></html>
1
$ 符号与其他框架的混合使用,产生的冲突的避免方式:
《1》jQuery 在其他库之后导入
method: 1
//将变量$的控制权,让渡给prototype.js(一种js框架)
jQuery.noConflict();
//使用 jQuery(), 代替$()方法
jQuery(function(){
//do some things
});method: 2
// 自定义一个快捷方式:
var $x = jQuery.noConflict();
$x (function(){
//do some things
//函数内部使用$x()方法,代替$()方法
})method: 3
//将变量$的控制权,让渡给prototype.js(一种js框架)
jQuery.noConflict();
//使用 jQuery设置页面加载时的执行函数
jQuery(function($){
//do some things
//函数内部正常使用$()方法
});method: 4
/*
最理想的方式,通过改变少量的代码,实现最全面的兼容性!
*/
//将变量$的控制权,让渡给prototype.js(一种js框架)
jQuery.noConflict();
//定义匿名函数,并设置形参为$
//匿名函数内部的$ == jQuery
(function($){
$(function(){
//do some things
//可以正常使用$()方法
});
})(jQuery);
//执行匿名函数,且传递实参jQuery《2》jQuery 在其他库之前导入
method: 5
//不需要调用jQuery.noConflict();
//可以使用$()方法作为其他框架/库的快捷方式
//直接使用 jQuery(), 代替$()方法
jQuery(function(){
//do some things
//使用 jQuery(), 代替$()方法
});
1
1
jQuery 使用注意事项 与 小技巧(tips)的更多相关文章
- 编写css相关注意事项以及小技巧
一.小技巧 1.对于开始写网站css之前一般都要对css进行重置(养成写注释的习惯): ;;} body{font-size:16px;} img{border:none;} li{list-styl ...
- 在Android中使用am和input命令在实际使用中的注意事项以及小技巧
在Android使用到am和进行一些操作是非常方便的,比如一个重复自动的操作,具体用来实现一些什么是看个人需求了,接下来说对于am和input的使用. 本文适用于已经大概去了解了am和input的朋友 ...
- 人人必知的10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. // Back t ...
- 26个jQuery使用小技巧(25)
下面列出了一些Jquery使用技巧.比如有禁止右键点击.隐藏搜索文本框文字.在新窗口中打开链接.检测浏览器.预加载图片.页面样式切换.所有列等高.动态控制页面字体大小.获得鼠标指针的X值Y值.验证元 ...
- Jquery 小技巧
[每个程序员都会的35个jQuery的小技巧]收集的35个jQuery的小技巧/代码片段,可以帮你快速开发
- 10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...
- 前端程序员应该知道的15个 jQuery 小技巧
下面这些简单的小技巧能够帮助你玩转jQuery. 返回顶部按钮 预加载图像 检查图像是否加载 自动修复破坏的图像 悬停切换类 禁用输入字段 停止加载链接 切换淡入/幻灯片 简单的手风琴 让两个div高 ...
- (译)你应该知道的jQuery小技巧
帮助提高你jQuery应用的简单小技巧 回到顶部按钮 图片预加载 判断图片是否加载完 自动修补破损图像 Hover切换class类 禁用输入 停止正在加载的链接 toggle fade/slide 简 ...
- 程序员都会的 35 个 jQuery 小技巧
收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 $(document).ready(function(){ $(document).bind("cont ...
随机推荐
- 云原生流水线 Argo Workflow 的安装、使用以及个人体验
注意:这篇文章并不是一篇入门教程,学习 Argo Workflow 请移步官方文档 Argo Documentation Argo Workflow 是一个云原生工作流引擎,专注于编排并行任务.它的特 ...
- 从零搭建一个IdentityServer——集成Asp.net core Identity
前面的文章使用Asp.net core 5.0以及IdentityServer4搭建了一个基础的验证服务器,并实现了基于客户端证书的Oauth2.0授权流程,以及通过access token访问被保护 ...
- Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf/
Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf ...
- How to kill go routine?
How to kill go routine? https://stackoverflow.com/questions/37997608/kill-a-method-in-an-infinite-lo ...
- Buffer Data RDMA 零拷贝 直接内存访问
waylau/netty-4-user-guide: Chinese translation of Netty 4.x User Guide. 中文翻译<Netty 4.x 用户指南> h ...
- 在不同情况下connect失败和ping不通的数据分析
- WebServices 与 Web API 的区别
WebServices : WebServices 是可以通过 Internet 访问并通过 XML 编码规范其通信的任何服务. 客户通过发送请求(大部分是 XML消息)来召唤 WebServices ...
- SpringCloud配置刷新机制的简单分析[nacos为例子]
SpringCloud Nacos 本文主要分为SpringCloud Nacos的设计思路 简单分析一下触发刷新事件后发生的过程以及一些踩坑经验 org.springframework.cloud. ...
- 框架spring+strutrs+ibatis
Tomcat加载完成 --- Web.xml --- sql-map-config.xml --- 读取xml(*-ibatis-config) --- Jsp的url --- action方法 -- ...
- Scala-文件操作
Scala-文件操作 一.遍历一个文件中的每一行 方法一: 使用Source.getLines返回的迭代器 方法二: 将Source.getLines返回的迭代器,转换成数组 方法三: 调用Sourc ...