1、把所有带有#的空链接换成不友情的链接
- $('a[href="#"]').each(function() {
- $(this).attr('href', 'javascript:void(0)')
- });
2、jQuery全选与取消全选插件
- (function($){
- $.fn.checkall = function(options){
- var defaults = {chname:"checkname[]", callback:null},
- options = $.extend(defaults, options),
- $obj = $(this),
- $items = $("input[name='"+options.chname+"']"),
- checkedItem = 0;
- $items.click(function(){
- if($items.filter(":checked").length === $items.length){
- $obj.attr("checked",true);
- }else{
- $obj.removeAttr("checked");
- }
- checkedItem = $items.filter(":checked").length;
- if(typeof options.callback === "function") options.callback(checkedItem);
- });
- return $obj.each(function(){
- $(this).click(function(){
- if($(this).attr("checked")){
- $items.attr("checked",true);
- }else{
- $items.removeAttr("checked");
- }
- checkedItem = $items.filter(":checked").length;
- if(typeof options.callback === "function") options.callback(checkedItem);
- });
- });
- }
- })(jQuery);
3、滚动条自动滚到顶部方法
- $("html,body").animate({scrollTop: 0}, "slow");
4、滚动条自动滚到底部方法
- var s = $("body").height()-$(window).height();
- $("html,body").animate({scrollTop: s}, "slow");
5、jQuery自动根据图片高度宽度缩
- jQuery.fn.ImageAutoSize = function(width,height){
- $(“img”,this).each(function(){
- var image = $(this);
- if(image.width()>width){
- image.width(width);
- image.height(width/image.width()*image.height());
- }
- if(image.height()>height){
- image.height(height);
- image.width(height/image.height()*image.width());
- }
- });
- }
调用:先引用上面的脚本或将上页的脚本放入自己的JS库,然后只要再加 $(function(){ $(“图片组所在的容器”).ImageAutoSize(限制最大宽,限制最大高);});
6、JQuery IFrame框架高度自适应(支持嵌套–兼容IE,ff,safafi,chrome)
- $("#IframeID").load(function() {
- $(this).height($(this).contents().height());
- })
有一点需要注意的,我也在调试的时候才发现的,耽误了不少时间。就是绑定事件必须在iframe加载完毕之前绑定,否则不会执行。
以下是jQuery,load事件的概述
在每一个匹配元素的load事件中绑定一个处理函数。
如果绑定给window对象,则会在所有内容加载后触发,包括窗口,框架,对象和图像。如果绑定在元素上,则当元素的内容加载完毕后触发。
注意:只有当在这个元素完全加载完之前绑定load的处理函数,才会在他加载完后触发。如果之后再绑定就永远不会触发了。所以不要在$(document).ready()里绑定load事件,因为jQuery会在所有DOM加载完成后再绑定load事件。
如果您觉得本文的内容对您的学习有所帮助:
关键字:
Jquery学堂 Jquery知识