[转]整理jquery开发技巧
1.创建一个嵌套的过滤器
1.$(jquery).filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素2.使用has()来判断一个元素是否包含特定的class或者元素
1.$("input").has(".email").addClass("email_icon");//如果class是email的,就添加email_iocn的class3.使用jQuery切换样式
1.$('link[media='screen']').attr('href', 'Alternative.css'); 4.限制选择的区域
1.var in_stock = $('#myid input.myclass');//找到id=myid元素里class=myclass的输入框5.正确使用ToggleClass
不使用情况:
1.a.hasClass('blueButton') ? a.removeClass('blueButton') : a.addClass('blueButton'); 使用ToggleClass
1.a.toggleClass('blueButton');6.浏览器判断
1.if($.browser.msie && $.browser.version > 6) {
//如果是IE6后的版本...
}7.查找一个元素的索引
1.$("ul > li").click(function () {
2. var index =
$(this).prevAll().length;
3.});8.使用jQuery预加载图片
1.$.preloadImages('image1.gif', '/path/to/image2.png', 'some/image3.jpg'); 9.自动的滚动到页面特定区域
1.$('.area_name').autoscroll();10. 关闭右键的菜单
1.$(document).bind('contextmenu',function(e){
return false; });11.判断一个元素是否存在
1.if($('#someDiv').length) {//存在}12.判断一个元素是否为空
1.if($('#keks').html()) { //为空} 13.判断鼠标的左右键点击
1.$("#someelement").live('click', function(e) { 2. if( (!$.browser.msie
&& e.button == 0) || ($.browser.msie && e.button == 1) ) {
3. alert("左键点击"); 4. } 5. else if(e.button == 2) 6. alert("右键点击"); 7.});14. 显示或者删除输入框的缺省值
01.$(".swap").each(function(i){ 02. $(this).focusin(function(){ 03. if ($(this).val() == "请输入") { 04. $(this).val("");
05. } 06. }).focusout(function(){ 07. if ($.trim($(this).val()) == "") {
08. $(this).val("请输入");
09. } 10. }); 11.});15.指定时间后自动隐藏或者关闭元素
1.$(".mydiv").delay(5000).hide('blind', {},
500);16.动态创建元素到DOM
1.varnewgbin1Div = $(''); 2.newgbin1Div.attr('id','gbin1.com').appendTo('body');17.使用jQuery克隆元素
1.varcloned = $('#gbin1div').clone();18.元素屏幕居中
1.jQuery.fn.center = function () { 2. this.css('position','absolute'); 3. this.css('top', ( $(window).height() - this.height() ) / +$(window).scrollTop()
+ 'px');
4. this.css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft()
+ 'px');return this;
5.}
6.$('#gbin1div').center();19.剔除元素中的HTML
01.(function($) { 02. $.fn.stripHtml = function() { 03. var regexp =
/<("[^"]*"|'[^']*'|[^'">])*>/gi;
04. this.each(function() { 05. $(this).html( 06. $(this).html().replace(regexp,"") 07. ); 08. }); 09. return $(this); 10. } 11.})(jQuery);
12.$('p').stripHtml();20.
使用closest来得到父元素
1.$('#searchBox').closest('div');21.使用firebug来记录jQuery事件
1.jQuery.log =
jQuery.fn.log = function (msg) { 2. if (console){
3. console.log("%s:
%o", msg, this); 4. } 5. return this; 6.};
7.$('#someDiv').hide().log('div hidden').addClass('someClass');22.点击链接强制弹出新窗口
1.Query('a.popup').live('click',
function(){
2. newwindow=window.open($(this).attr('href'),'','height=200,width=150');
3. if (window.focus)
{newwindow.focus()} 4. return false; 5.});23.点击链接强制打开新标签页
1.jQuery('a.newTab').live('click',
function(){
2. newwindow=window.open($(this).href); 3. jQuery(this).target = "_blank"; 4. return false; 5.});24.取得鼠标的X和Y坐标
1.$(document).mousemove(function(e){ 2.$(document).ready(function() { 3.$().mousemove(function(e){ 4.$('#XY').html("Gbin1 X Axis : " + e.pageX + " | Gbin1 Y Axis " + e.pageY);
5.});
6.});25.解析XML
1.function parseXml(xml) {
2. //find every Tutorial and print the author
3. $(xml).find("Tutorial").each(function() 4. { 5. $("#output").append($(this).attr("author")
+ "");
6. }); 7.}26.判断一个图片是否加载完全
1.$('#theGBin1Image').attr('src', 'image.jpg').load(function() { 2.alert('This Image Has Been
Loaded'); 3.});[转]整理jquery开发技巧的更多相关文章
- 15个值得开发人员关注的jQuery开发技巧和心得
在这篇文章中,我们将介绍15个让你的jQuery更加有效的技巧,大部分关于性能提升的,希望大家能够喜欢! 1. 尽量使用最新版本的jQuery类库 jQuery项目中使用了大量的创新.最好的方法来提高 ...
- jQuery开发技巧
jQuery 事件 - submit() 方法 $("form").submit(function(e){}); 当提交表单时,会发生 submit 事件. 该事件只适用于表单元素 ...
- 经典收藏 50个jQuery Mobile开发技巧集萃
http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...
- (转)经典收藏 50个jQuery Mobile开发技巧集萃
(原)http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 经典收藏 50个jQuery Mobile开发技巧集萃 ...
- jQuery常用技巧-使用的总结
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- jQuery常用技巧
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用 ...
- 人人必知的10个 jQuery 小技巧
原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧 收集的10个 jQuery ...
- SQL开发技巧(二)
本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列文章基于SQLServer系列,且版本为SQLServer2005及以上-- 文章系列目录 SQL开发技巧(一) SQL开 ...
- 10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...
随机推荐
- zabbix 布署实践【4 服务器自动探索发现,并且自动关联模版】
使用管理员登录zabbix后,在配置---自动发现--创建发现规则 如下:我们的需求是监听办公网内openstack的所有虚拟机,在其VM创建后,自动加到zabbix监控中来,并自动关联监控模版 可以 ...
- apache 配置静态文件缓存和开启gzip压缩
1,设置文件静态缓存3天: 在httpd.conf 里添加如下代码: #文件静态缓存配置 <IfModule expires_module> #打开缓存 ExpiresActive on ...
- php取整
php取整的方法一共有4中,分别是ceil(),floor(),round(),intval(). 1.ceil--向上取整,即不小于当前的下一个整数,如果有小数则进一位. 返回的是float类型 & ...
- 新Mac 开机启动MySQL/MongoDB/Redis 等服务
在Mac上我们使用[homebrew]包管理工具(http://brew.sh/index_zh-cn.html)来安装和管理开发工具包,例如:mysql.php.redis.只需要一个命令 brew ...
- Chapter 2 Open Book——34
His gaze became appraising. "You put on a good show," he said slowly. 他的凝视变成了评价.“你上演了一场好戏” ...
- gulp插件大全
原文:http://www.mamicode.com/info-detail-517085.html No.1.run-sequence 作用:让gulp任务,可以相互独立,解除任务间的依赖,增强 ...
- redisTemplate keys方法 为空
我遇到的原因是spring.xml配置有问题,应该为: <bean id="redisTemplate" class="org.springframework.da ...
- HTML5历史管理
边看视频边做的练习,随机显示数字,分别使用history和hash来实现历史管理 <!doctype html> <html> <head> <meta ch ...
- emmet 教程 emmet快捷键大全
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- github 上传文件
1.删除项目 2. 包管理器初始化 npm init name 必填项 后面可一直回车 最后选择yes 3.建立本地仓储 在git bash 中输入命令 git init 4. 添加 ...