jQuery.fn.autoscroll = function(selector) { $('html,body').animate( {scrollTop: $(selector).offset().top}, ); } //Then to scroll to the class/area you wish to get to like this: $('.area_name').autoscroll();
19.检测各种浏览器
Detect Safari (if( $.browser.safari)), Detect IE6 and over (if ($.browser.msie && $.browser.version > 6 )), Detect IE6 and below (if ($.browser.msie && $.browser.version <= 6 )), Detect FireFox 2 and above (if ($.browser.mozilla && $.browser.version >= '1.8' )
20.限制textarea的字符数量
jQuery.fn.maxLength = function(max){ this.each(function(){ var type = this.tagName.toLowerCase(); var inputType = this.type? this.type.toLowerCase() : null; if(type == "input" && inputType == "text" || inputType == "password"){ //Apply the standard maxLength this.maxLength = max; } else if(type == "textarea"){ this.onkeypress = function(e){ var ob = e || event; var keyCode = ob.keyCode; var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd; return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection); }; this.onkeyup = function(){ if(this.value.length > max){ this.value = this.value.substring(0,max); } }; } }); }; //Usage: $('#gbin1textarea').maxLength(500);
21.使用jQuery克隆元素
var cloned = $('#gbin1div').clone();
22. 元素屏幕居中
jQuery.fn.center = function () { this.css('position','absolute'); this.css('top', ( $(window).height() - this.height() ) / +$(window).scrollTop() + 'px'); this.css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px');return this; } //Use the above function as: $('#gbin1div').center();
23 .简单的tab标签切换
jQuery('#meeting_tabs ul li').click(function(){ jQuery(this).addClass('tabulous_active').siblings().removeClass('tabulous_active'); jQuery('#tabs_container>.pane:eq('+jQuery(this).index()+')').show().siblings().hide(); }) <div> <ul> <li><a href="#" title="">进行中</a></li> <li><a href="#" title="">未开始</a></li> <li><a href="#" title="">已结束</a></li> <li><a href="#" title="">全部</a></li> </ul> <div> <div >1</div> <div >2</div> <div >3</div> <div >4</div> </div> </div>
以上这篇jQuery 常用代码集锦(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章: