1.创建一个嵌套的过滤器

 
1.$(jquery).filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素

2.使用has()来判断一个元素是否包含特定的class或者元素

 
1.$("input").has(".email").addClass("email_icon");//如果class是email的,就添加email_iocn的class

3.使用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.var
newgbin1Div = $('');
2.newgbin1Div.attr('id','gbin1.com').appendTo('body');

17.使用jQuery克隆元素

1.var
cloned = $('#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开发技巧的更多相关文章

  1. 15个值得开发人员关注的jQuery开发技巧和心得

    在这篇文章中,我们将介绍15个让你的jQuery更加有效的技巧,大部分关于性能提升的,希望大家能够喜欢! 1. 尽量使用最新版本的jQuery类库 jQuery项目中使用了大量的创新.最好的方法来提高 ...

  2. jQuery开发技巧

    jQuery 事件 - submit() 方法 $("form").submit(function(e){}); 当提交表单时,会发生 submit 事件. 该事件只适用于表单元素 ...

  3. 经典收藏 50个jQuery Mobile开发技巧集萃

    http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...

  4. (转)经典收藏 50个jQuery Mobile开发技巧集萃

    (原)http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 经典收藏 50个jQuery Mobile开发技巧集萃   ...

  5. jQuery常用技巧-使用的总结

    1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...

  6. jQuery常用技巧

      1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用 ...

  7. 人人必知的10个 jQuery 小技巧

    原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧   收集的10个 jQuery ...

  8. SQL开发技巧(二)

    本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列文章基于SQLServer系列,且版本为SQLServer2005及以上-- 文章系列目录 SQL开发技巧(一) SQL开 ...

  9. 10个jQuery小技巧

    收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...

随机推荐

  1. Latex—IEEE Latex模板 期刊名带下划线的问题解决

    其实期刊名应该是斜体字的,但是有可能默认模板会导致斜体变下划线的问题,解决方法如下 引用包: \usepackage{ulem} %to strike the words 然后再在: \bibliog ...

  2. js splice比较好用的方法

    http://www.w3school.com.cn/jsref/jsref_splice.asp从w3c看到这个方法,感觉不错,记录一下.

  3. CODE[VS]-蛇形矩阵-模拟-天梯白银

    题目描述 Description 小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1开始以逆时针方向绕行,逐圈扩大,直到n行n列填满数字,请输出该 ...

  4. homebrew for mac

    注意 如果bash_profile 文件路径写错了,而导致很多命令不能使用 可以在终端 /usr/bin/vim ~/.bash_profile    打开编辑  esc退出  按冒号(:)再按wq ...

  5. IMacro 脚本简记

    抓取速卖通纠纷订单详情,生成csv进行统计. var macro1="CODE:";macro1+="VERSION BUILD=8970419 RECORDER=FX& ...

  6. asp.net——初识多线程

    1.首先讲解一下什么是线程(该定义是参考线程的百度百科) 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针( ...

  7. 梅特卡夫法则(Metcalfe's law)

    如果一个网络中有n个人,那么网络对于每个人的价值与网络中其他人的数量成正比,于是网络对于所有人的总价值与n*(n-1)成正比.

  8. 圆形图片CustomShapeImageView

    第三方控件 [GitHub的源码下载] (https://github.com/MostafaGazar/CustomShapeImageView) 1:依赖包 dependencies { ... ...

  9. ElasticSearch(7)-排序

    引用自ElaticSearch权威指南 一.排序 相关性排序 默认情况下,结果集会按照相关性进行排序 -- 相关性越高,排名越靠前. 这一章我们会讲述相关性是什么以及它是如何计算的. 在此之前,我们先 ...

  10. java 时间

    package com.grace.test; import java.text.DateFormat; import java.text.ParseException; import java.te ...