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 ...
随机推荐
- apache状态显示报错AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdo...is message
今天启动apache查看状态发现报错,说不能确认服务器完全确认域名,以下是报错内容: [root@localhost ~]# service httpd status Redirecting to / ...
- C#高级编程第11版 - 第九章 索引
[1]9.1 System.String 类 String类中关键的方法.如替换,比较等. [2]9.1.1 构建字符串 1.String类依然有一个缺点:因为它是不可变的数据类型,这意味当你初始化一 ...
- .net core 和 WPF 开发升讯威在线客服与营销系统:使用 TCP协议 实现稳定的客服端
本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...
- Redis持久化之RDB和AOF
Redis是一个键值对数据库服务器,由于Redis是内存数据库,那么有很多内存的特点,例如掉电易失,或者进程退出,服务器中的数据也将消失不见,所以需要一种方法将数据从内存中写到磁盘,这一过程称之为数据 ...
- # Set the asyncio reactor's event loop as global # TODO: Should we instead pass the global one into the reactor?
daphne/server.py at master · django/daphne https://github.com/django/daphne/blob/master/daphne/serve ...
- 机器学习基础——规则化(Regularization)
在机器学习中,我们一直期望学习一个泛化能力(generalization)强的函数只有泛化能力强的模型才能很好地适用于整个样本空间,才能在新的样本点上表现良好. \[y=a+bx+cx^2+dx^3\ ...
- XV6学习(9)Lab cow: Copy-on-write fork
代码在github上.总体来说如果理解了COW机制的话,这个实验的完成也没有很复杂. 这一个实验是要完成COW(copy on write)fork.在原始的XV6中,fork函数是通过直接对进程的地 ...
- Java并发练习
1.按顺序打印ABC 三个线程,每个线程分别打印A,B,C各十次,现在要求按顺序输出A,B,C package concurrency; import java.util.concurrent.Exe ...
- Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警
Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...
- io流读写操作
/** * * DOC 将F盘下的test.jpg文件,读取后,再存到E盘下面. * * @param args * @throws Exception */ public static void m ...