下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
1. 导航菜单背景切换效果
<ul id='nav'> <li>导航一</li> <li>导航二</li> <li>导航三</li> </ul>
//注意:代码需要修饰完善 $('#nav').click(function(e) { // 要知道siblings的使用 $(e.target).addClass('tclass').siblings('.tclass').removeClass('tclass');; });
2.反序访问JQuery对象里的元素
//要掌握JQuery对象的get方法 以及数组的reverse方法即可 var arr = $('#nav').find('li').get().reverse(); $.each(arr,function(index,ele){ .... ... });
3.访问IFrame里的元素
var iFrameDOM = $("iframe#someID").contents(); //然后,就可以通过find方法来遍历获取iFrame中的元素了 iFrameDOM.find(".message").slideUp();
4. 管理搜索框的值
$("#searchbox") .focus(function(){$(this).val('')}) .blur(function(){ var $this = $(this); // '请搜索...'为搜索框默认值 ($this.val() === '')? $this.val('请搜索...') : null; });
5.部分页面加载更新
setInterval(function() { //每隔5秒钟刷新页面内容 //获取的内容将增加到 id为content的元素后 $("#content").load(url); },5000);
6.采用data方法来缓存数据
var cache = {}; $.data(cache,'key','value'); //缓存数据 //获取数据 $.data(cache,'key');
7.采配置JQuery与其它库的兼容性
//方法一: 为JQuery重新命名为 $j var $j = jQuery.noConflict(); $j('#id').... //方法二: 推荐使用的方式 (function($){ $(document).ready(function(){ //这儿,你可以正常的使用JQuery语法 }); })(jQuery);
8.克隆table header到表格的最下面
var $tfoot = $('<tfoot></tfoot>'); $($('thead').clone(true,true).children().get().reverse()).each(function(){ $tfoot.append($(this)); }); $tfoot.insertAfter('table thead');
9. 根据视窗(viewport)创建一个全屏宽度和高度(width/height)的div
$('#content').css({ 'width': $(window).width(),'height': $(window).height(),}); // make sure div stays full width/height on resize $(window).resize(function(){ var $w = $(window); $('#content').css({ 'width': $w.width(),'height': $w.height(),}); });
10 测试密码的强度
<input type="password" name="pass" id="pass" /> <span id="passstrength"></span>
//下面的正则表达式建议各位收藏哦,项目上有可能会用得着 $('#pass').keyup(function(e) { //密码为八位及以上并且字母数字特殊字符三项都包括 var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$","g"); //密码为七位及以上并且字母、数字、特殊字符三项中有两项,强度是中等 var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$","g"); var enoughRegex = new RegExp("(?=.{6,}).*","g"); if (false == enoughRegex.test($(this).val())) { $('#passstrength').html('More Characters'); } else if (strongRegex.test($(this).val())) { $('#passstrength').className = 'ok'; $('#passstrength').html('Strong!'); } else if (mediumRegex.test($(this).val())) { $('#passstrength').className = 'alert'; $('#passstrength').html('Medium!'); } else { $('#passstrength').className = 'error'; $('#passstrength').html('Weak!'); } return true; });
11.使用JQuery重绘图片的大小
$(window).bind("load",function() { // IMAGE RESIZE $('#product_cat_list img').each(function() { var maxWidth = 120; var maxHeight = 120; var ratio = 0; var width = $(this).width(); var height = $(this).height(); if(width > maxWidth){ ratio = maxWidth / width; $(this).css("width",maxWidth); $(this).css("height",height * ratio); height = height * ratio; } var width = $(this).width(); var height = $(this).height(); if(height > maxHeight){ ratio = maxHeight / height; $(this).css("height",maxHeight); $(this).css("width",width * ratio); width = width * ratio; } }); //$("#contentpage img").show(); // IMAGE RESIZE });
12.滚动时动态加载页面内容
var loading = false; $(window).scroll(function(){ if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){ if(loading == false){ loading = true; $('#loadingbar').css("display","block"); $.get("load.php?start="+$('#loaded_max').val(),function(loaded){ $('body').append(loaded); $('#loaded_max').val(parseInt($('#loaded_max').val())+50); $('#loadingbar').css("display","none"); loading = false; }); } } }); $(document).ready(function() { $('#loaded_max').val(50); });
来自: http://www.cnblogs.com/txiaoxin/p/4928320.html
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。