范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

(网页)人人都会的35个Jquery小技巧

  收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发.
  1. 禁止右键点击 $(document).ready(function(){     $(document).bind("contextmenu",function(e){         return false;     }); });
  2. 隐藏搜索文本框的文字 Hide when clicked in the search field, the value.(example can be found below in the comment fields)  $(document).ready(function() { $("input.text1").val("Enter your search text here");    textFill($("input.text1")); });       function textFill(input){ //input focus text function      var originalvalue = input.val();      input.focus( function(){           if( $.trim(input.val()) == originalvalue ){ input.val(""); }      });      input.blur( function(){           if( $.trim(input.val()) == "" ){ input.val(originalvalue); }      }); }
  3. 在新窗口中打开链接 XHTML 1.0 Strict doesn’t allow this attribute in the code, so use this to keep the code valid.  $(document).ready(function() {    //Example 1: Every link will open in a new window    $("a[href^="http://"]").attr("target", "_blank");      //Example 2: Links with the rel="external" attribute will only open in a new window    $("a[@rel$="external"]").click(function(){       this.target = "_blank";    }); }); // how to use open link
  4. 检测浏览器
  注: 在版本jQuery 1.4中,$.support 替换掉了$.browser 变量 $(document).ready(function() { // Target Firefox 2 and above if ($.browser.mozilla && $.browser.version >= "1.8" ){     // do something }  // Target Safari if( $.browser.safari ){     // do something }  // Target Chrome if( $.browser.chrome){     // do something }  // Target Camino if( $.browser.camino){     // do something }  // Target Opera if( $.browser.opera){     // do something }  // Target IE6 and below if ($.browser.msie && $.browser.version <= 6 ){     // do something }  // Target anything above IE6 if ($.browser.msie && $.browser.version > 6){     // do something } });
  5. 预加载图片
  This piece of code will prevent the loading of all images, which can be useful if you have a site with lots of images. $(document).ready(function() { jQuery.preloadImages = function() {   for(var i = 0; i").attr("src", arguments[i]);   } } // how to use $.preloadImages("image1.jpg"); });
  6. 页面样式切换 $(document).ready(function() {     $("a.Styleswitcher").click(function() {         //swicth the LINK REL attribute with the value in A REL attribute         $("link[rel=stylesheet]").attr("href" , $(this).attr("rel"));     }); // how to use // place this in your header  // the links Default Theme Red Theme Blue Theme });
  7. 列高度相同
  如果使用了两个CSS列,使用此种方式可以是两列的高度相同。 $(document).ready(function() { function equalHeight(group) {     tallest = 0;     group.each(function() {         thisHeight = $(this).height();         if(thisHeight > tallest) {             tallest = thisHeight;         }     });     group.height(tallest); } // how to use $(document).ready(function() {     equalHeight($(".left"));     equalHeight($(".right")); }); });
  8. 动态控制页面字体大小
  用户可以改变页面字体大小 $(document).ready(function() {   // Reset the font size(back to default)   var originalFontSize = $("html").css("font-size");     $(".resetFont").click(function(){     $("html").css("font-size", originalFontSize);   });   // Increase the font size(bigger font0   $(".increaseFont").click(function(){     var currentFontSize = $("html").css("font-size");     var currentFontSizeNum = parseFloat(currentFontSize, 10);     var newFontSize = currentFontSizeNum*1.2;     $("html").css("font-size", newFontSize);     return false;   });   // Decrease the font size(smaller font)   $(".decreaseFont").click(function(){     var currentFontSize = $("html").css("font-size");     var currentFontSizeNum = parseFloat(currentFontSize, 10);     var newFontSize = currentFontSizeNum*0.8;     $("html").css("font-size", newFontSize);     return false;   }); });
  9. 返回页面顶部的功能
  For a smooth(animated) ride back to the top(or any location). $(document).ready(function() { $("a[href*=#]").click(function() {  if (location.pathname.replace(/^//,"") == this.pathname.replace(/^//,"")  && location.hostname == this.hostname) {    var $target = $(this.hash);    $target = $target.length && $target    || $("[name=" + this.hash.slice(1) +"]");    if ($target.length) {   var targetOffset = $target.offset().top;   $("html,body")   .animate({scrollTop: targetOffset}, 900);     return false;    }   }   }); // how to use // place this where you want to scroll to  // the link go to top });
  10. 获得鼠标指针XY值
  Want to know where your mouse cursor is? $(document).ready(function() {    $().mousemove(function(e){      //display the x and y axis values inside the p with the id XY     $("#XY").html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);   }); // how to use   });
  11.返回顶部按钮
  你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件。 // Back to top $("a.top").click(function () {   $(document.body).animate({scrollTop: 0}, 800);   return false; });  Back to top
  改变 scrollTop 的值可以调整返回距离顶部的距离,而 animate 的第二个参数是执行返回动作需要的时间(单位:毫秒)。
  12.预加载图片
  如果你的页面中使用了很多不可见的图片(如:hover 显示),你可能需要预加载它们: $.preloadImages = function () {   for (var i = 0; i < arguments.length; i++) {     $("").attr("src", arguments[i]);   } };  $.preloadImages("img/hover1.png", "img/hover2.png");
  13.检查图片是否加载完成
  有时候你需要确保图片完成加载完成以便执行后面的操作: $("img").load(function () {   console.log("image load successful"); });
  你可以把 img 替换为其他的 ID 或者 class 来检查指定图片是否加载完成。
  14.自动修改破损图像
  如果你碰巧在你的网站上发现了破碎的图像链接,你可以用一个不易被替换的图像来代替它们。添加这个简单的代码可以节省很多麻烦: $("img").on("error", function () {   $(this).prop("src", "img/broken.png"); });
  即使你的网站没有破碎的图像链接,添加这段代码也没有任何害处。
  15.鼠标悬停(hover)切换 class 属性
  假如当用户鼠标悬停在一个可点击的元素上时,你希望改变其效果,下面这段代码可以在其悬停在元素上时添加 class 属性,当用户鼠标离开时,则自动取消该条件 class 属性: $(".btn").hover(function () {   $(this).addClass("hover");   }, function () {     $(this).removeClass("hover");   });
  你只需要添加必要的CSS代码即可。如果你想要更简洁的代码,可以使用 toggleClass 方法: $(".btn").hover(function () {    $(this).toggleClass("hover");  });
  注:直接使用CSS实现该效果可能是更好的解决方案,但你仍然有必要知道该方法。
  16.禁用 input 字段
  有时你可能需要禁用表单的 submit 按钮或者某个 input 字段,直到用户执行了某些操作(例如,检查"已阅读条款"复选框)。可以添加 disabled 属性,直到你想启用它时: $("input[type="submit"]").prop("disabled", true);
  你要做的就是执行 removeAttr 方法,并把要移除的属性作为参数传入: $("input[type="submit"]").removeAttr("disabled");
  17.阻止链接加载
  有时你不希望链接到某个页面或者重新加载它,你可能希望它来做一些其他事情或者触发一些其他脚本,你可以这么做: $("a.no-link").click(function (e) {   e.preventDefault(); });
  18.切换 fade/slide
  fade 和 slide 是我们在 jQuery 中经常使用的动画效果,它们可以使元素显示效果更好。但是如果你希望元素显示时使用第一种效果,而消失时使用第二种效果,则可以这么做: // Fade $(".btn").click(function () {   $(".element").fadeToggle("slow"); }); // Toggle $(".btn").click(function () {   $(".element").slideToggle("slow"); });
  19.简单的手风琴效果
  这是一个实现手风琴效果快速简单的方法: // Close all panels $("#accordion").find(".content").hide(); // Accordion $("#accordion").find(".accordion-header").click(function () {   var next = $(this).next();   next.slideToggle("fast");   $(".content").not(next).slideUp("fast");   return false; });
  20.让两个 DIV 高度相同
  有时你需要让两个人 p 高度相同,而不管它们里面的内容多少。可以使用下面的代码片段: var $columns = $(".column"); var height = 0; $columns.each(function () {   if ($(this).height() > height) {     height = $(this).height();   } }); $columns.height(height);
  这段代码会循环一组元素,并设置它们的高度为元素中的最大高度。
  21. 验证元素是否为空
  This will allow you to check if an element is empty. $(document).ready(function() {   if ($("#id").html()) {    // do something    } });
  22. 替换元素
  Want to replace a p, or something else? $(document).ready(function() {    $("#id").replaceWith(" I have been replaced  "); });
  23. jQuery延时加载功能
  Want to delay something? $(document).ready(function() {    window.setTimeout(function() {      // do something    }, 1000); });
  24. 移除单词功能
  Want to remove a certain word(s)? $(document).ready(function() {    var el = $("#id");    el.html(el.html().replace(/word/ig, "")); });
  25. 验证元素是否存在于jquery对象集合中
  Simply test with the .length property if the element exists. $(document).ready(function() {    if ($("#id").length) {   // do something   } });
  26. 使整个DIV可点击
  Want to make the complete p clickable? $(document).ready(function() {     $("p").click(function(){       //get the url from href attribute and launch the url       window.location=$(this).find("a").attr("href"); return false;     }); // how to use home  });
  27. ID与Class之间转换
  当改变Window大小时,在ID与Class之间切换 $(document).ready(function() {    function checkWindowSize() {     if ( $(window).width() > 1200 ) {         $("body").addClass("large");     }     else {         $("body").removeClass("large");     }    } $(window).resize(checkWindowSize); });
  28. 克隆对象
  Clone a p or an other element. $(document).ready(function() {    var cloned = $("#id").clone(); // how to use   });
  29. 使元素居屏幕中间位置
  Center an element in the center of your screen. $(document).ready(function() {   jQuery.fn.center = function () {       this.css("position","absolute");       this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");       this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");       return this;   }   $("#id").center(); });
  30. 写自己的选择器
  Write your own selectors. $(document).ready(function() {    $.extend($.expr[":"], {        moreThen1000px: function(a) {            return $(a).width() > 1000;       }    });   $(".box:moreThen1000px").click(function() {       // creating a simple js alert box       alert("The element that you have clicked is over 1000 pixels wide");   }); });
  31. 统计元素个数
  Count an element. $(document).ready(function() {    $("p").size(); });
  32. 使用自己的 Bullets
  Want to use your own bullets instead of using the standard or images bullets? $(document).ready(function() {    $("ul").addClass("Replaced");    $("ul > li").prepend("‒ ");  // how to use  ul.Replaced { list-style : none; } });
  33. 引用Google主机上的Jquery类库
  Let Google host the jQuery script for you. This can be done in 2 ways. //Example 1     // Example 2:(the best and fastest way)
  34. 禁用Jquery(动画)效果
  Disable all jQuery effects $(document).ready(function() {     jQuery.fx.off = true; });
  35. 与其他Javascript类库冲突解决方案
  To avoid conflict other libraries on your website, you can use this jQuery Method, and assign a different variable name instead of the dollar sign. $(document).ready(function() {    var $jq = jQuery.noConflict();    $jq("#id").show(); });

7月轿车销量多车型大幅度下跌,天籁上榜宏光MINI夺亚军近日,中国汽车流通协会汽车市场研究分会(乘联会)公布了最新零售销量数据统计。据数据显示,7月国内狭义乘用车销量达150。0万辆,同比下降6。2,环比下降4。9。7月份汽车市场销量大如果将这些车型复活,现在它们就是yyds?有天,我无意中打开抽屉,找到那台曾经用过的录音机,按起播放音乐按钮,回想起当年的人和事,嘴角总是会情不自禁地微微上扬,脑海中满是画面。我承认,我是一个有情怀的人,虽然这台录音机已经中国汽车产业发展论坛再次强调全力发展新能源汽车在2021年的9月4日,由中国汽车技术研究中心有限公司中国汽车工程学会中国汽车工业协会中国汽车报社等多家工业协会联合举办了第十七届中国汽车产业发展(泰达)国际论坛,本论坛围绕融合创大众汽车将不再推手动挡车型,还有必要考C1驾照吗?近日,有外媒报道,大众汽车正在签署手动变速箱的死刑令。大众汽车预计在2023年后在欧洲发布的新车型不会再推出手动挡配置。此外,到2030年后,大众汽车这一计划将会覆盖美国和中国市场福特汽车因高额税收撤出印度,自主品牌该不该冒险进军?9月10日,美国汽车生产制造商福特汽车突然宣布,将撤出公司在印度的生产线。据福特汽车公司首席执行官吉姆法利表示,为了能够实现公司长期增长目标,福特汽车在印度的生产将立即结束,这项措未来手机都该这样,真正的屏下摄像头手机,中兴Axon30屏下版发布7月27日,全新一代屏下摄像手机中兴Axon305G正式发布。作为中兴手机重磅力作,中兴Axon305G屏下摄像技术进一步升级,以商用再度领跑市场的硬核实力开启全屏时代新格局。延续朱耿洲资本时代账房先生还有立足之地吗?在市场经济发达国家,会计师律师和医师是三个高收入的智力密集型职业,会计师曾被称作金饭碗,越老越吃香的热门职业。但随着市场经济的发展,对会计人员的要求越来越高,会计人员的角色已不能满71岁上班,96岁失恋,100岁获奖,她的人生有多高级对于她来说,时间是幻象,年龄是幻象。71岁上班,96岁失恋,100岁拿奖,100多岁还在创造奇迹这位大女孩,105岁还在喝酒吃肉,人生忙到没时间思考死亡她的人生经历一定能给你启发,千锤百炼出英才资本策划师(CCP)颁证木棉花盛开的暖春时节,3月26日,资本策划研究院资本策划师协会在广州举行了隆重喜庆而热烈的资本策划师(CCP)资质认证证书颁证议式。资本策划师(CCP)创始人知识产权拥有者资本策划每个人都应该是自己的资本策划师(CCP)又是三月百花香,又是新航启程时。三月,全国两会亮出十四五规划的宏伟蓝图三月,中国资本策划研究院也迎来第二批资本策划师(CCP)认证学员,CCP队伍不断发展壮大,为破解企业的融资难添深扒中兴Axon30比小米还会玩性价比的屏下手机,究竟怎么样?自从智能手机诞生,手机屏幕就一直是兵家必争之地,从屏幕尺寸,到分辨率,再到刷新率,以及如今的全面屏概念,手机厂商一直致力于将更好的显示效果呈现给用户。基于对手机屏占比的执着追求,中
昨天面试被问到的缓存淘汰算法FIFOLRULFU及Java实现缓存淘汰算法在高并发高性能的质量要求不断提高时,我们首先会想到的就是利用缓存予以应对。第一次请求时把计算好的结果存放在缓存中,下次遇到同样的请求时,把之前保存在缓存中的数据直接拿来工信部部长肖亚庆深入开展APP整治,对移动互联网服务涉及的诸多环节进行全链条全覆盖监管金融界2月28日消息工信部部长肖亚庆今日出席发布会表示,我为群众办实事实践活动,突出解决了APP治理和适老化改造两个方面的问题。在APP治理方面,通过制定标准技术检验专项整治行业自互联网信息服务算法推荐管理规定明起正式施行国家网信办等四部门联合发布的互联网信息服务算法推荐管理规定,将于明天(3月1日)起正式施行。对于普通用户而言,算法摸不着也看不懂,却在广泛影响着我们通过各种平台获取互联网信息服务。自己买监控摄像头容易安装吗自己买一个监控摄像头想要安装在家里,平时没事就看看实时监控,第一是防盗,第二是注意家人的安全尤其是家里有老人小孩,第三就是看看是不是有陌生人出入。那么自己买到的监控摄像头容易安装吗2月份各价位最值得买的手机全在这里!1199元也能越级体验旗舰机2月快要结束了,本月发布了不少机型,很多朋友挑花了眼,担心买贵不划算,更担心挑的不是合适自己的,买了会后悔。今天快哥整理了2月份各价位最值得买的手机供大家参考,从千元机到六千以上的入冬必备法宝飞利浦暖风机每年深圳的年后总有一段时间的寒潮,加上下雨简直是冷到刺骨啊!这几天78终于还是扛不住决定买个取暖机。在网上搜索对比了很多家,作为一个打工党的我入了台飞利浦暖风机,毕竟是大品牌有保障从3799降到2409,12GB256GB,雷军卖力宣传的手机前不久,红米给广大消费者带来了一款自诩为最冷骁龙8的旗舰手机红米K50电竞版。看到红米K50电竞版这款手机的最终定价之后,很多小伙伴都纷纷表示非常失望。原因无它,就是红米K50电竞呼吁大厂停止内卷,回归常态没想到吧,互联网大厂带头反内卷了。前几天,一条关于微信1065强制员工下班的新闻冲上热搜。所谓1065,是指早上10点下班,晚上6点下班,一周工作5天,这个很好理解。最戳人眼球的,只卖15来万!2022款奇瑞新能源大蚂蚁在新能源市场爆发的今天,如何打造一款能够以实力征服诸多挑剔眼光的产品,是个愈发值得思考的问题。作为基于LIFE绿色智慧模块化技术平台研发的新款纯电SUV,2022款奇瑞新能源大蚂蚁上市途中宝丰新能源大幅增资此前获宁夏宝丰系光伏资产注入宁夏首富党彦宝旗下已启动上市的光伏企业宝丰新能源日前完成增资。宁夏宝丰新能源科技股份有限公司(宝丰新能源)工商信息显示,其于2月24日发生股权变更,其中注册资本由13。2亿元增至1明年取消新能源补贴或收取购置附加税你还会考虑电动车吗?新能源的补贴,已经到了年终末尾,据说这是补贴的最后一个年头,到了明年,一切都只是按照标价出售!如果是这样,那么你还愿意买电动车吗?如果你已经忘记了补贴的政策,那我们来回忆一下。20