2009-01-11 73 views
12

与SO上的c#扩展主题类似,如果我们可以把大量有用的jQuery函数扩展集合在一起,那将是非常棒的。为你的jQuery发布你的短扩展函数好东西

请注意这个想法是有代码没有完全成熟的知名插件或UI部件

+0

该网站的结构是这样的,使用适当的标签和标题,可以找到特定的问题和答案,为特定的搜索。如果我需要一个日志插件,我不会搜索“[jquery]扩展程序好东西”,我会搜索“[jquery-plugin] logging”或搜索jQuery插件存储库。 – Shog9 2009-01-11 17:38:16

+0

有趣的是如何得到3000视图然后http://stackoverflow.com/questions/271398/post-your-extension-goodies-for-c-net-codeplex-com-extensionoverflow。 – redsquare 2009-01-11 17:42:03

+0

Shog - 这个项目变成了一个完整的项目,所以我想你必须从你认为的那个崇高的鲈鱼跳下来。 – redsquare 2009-01-11 17:48:17

回答

10
// Allows chainable logging 
// USAGE: $('#someDiv').hide().log('div hidden').addClass('someClass'); 
// Demo : http://jsbin.com/odeke 
jQuery.log = jQuery.fn.log = function (msg) { 
     if (window.console && window.console.log) { 
     console.log("%s: %o", msg, this); 
     } 
     return this; 
}; 
1

validation插件是真棒短的片段。在ASP.NET MVC应用程序中使用它来动态验证客户端上的东西使用Ajax ...甚至基于用户输入返回的自定义错误消息...非常酷。

-2

只是获取/设置元素ID的快捷方式。

(function($) { 
    $.fn.id = function(newDOMID){ 
     var $this = $(this); 
     if($this.attr('id')){ 
      if(!newDOMID){ 
       $this.id.getID($this); 
      } 
      else { 
       $this.id.setID($this,newDOMID); 
      } 
     } 
     else { 
      alert('The target no longer appears to be a part of the DOM!') 
     } 
    }; 
    $.fn.id.getID = function($this){ 
     return $this.attr('id'); 
    }; 
    $.fn.id.setID = function($this,newDOMID){ 
     $this.attr('id',newDOMID); 
     return this 
    }; 
})(jQuery); 

它是jQuery插件网站上的jID。

4

您可以使用它来查看选择器是否存在。

if($.exists('#mydiv')) { } 

$.exists = function(selector) { 
    return ($(selector).length); 
} 
0

啊 我有点掉最初的问题,但如果有“获取/设置一个ID”片断,然后 我有一些代码来创建唯一的ID:

$.increment = function(){ 
    var me = arguments.callee; 
    if (!me.count) me.count = 0; 
    return ++me.count;  
} 

$.domToSelector = function (jq, options){ 
    var selectors = [], i = 0; defaults = {}, opts = $.extend(defaults,options); 
    $(jq).each(function(){ 
     var $node = $(this);  
     if ($node.attr('id')){ 
      selectors[i] = '#'+$(this).attr('id');  
     } 
     else{ 
      var customId = ''+new Date; 
      customId = customId.replace(/ /g, '').replace(/:/g, '').replace(/\+/g, ''); 
      customId = customId+'_'+$.increment(); 
      if (opts.prefix) customId = opts.prefix+customId; 
      $node.attr('id', customId); 
      selectors[i] = '#'+customId;   
     } 
     i++;  
    }); 
    if (selectors.length == 1) selectors = selectors[0];  
    return selectors; 
} 
1

快速,方便AJAX:

下让你做出像锚

<a href='http://www.google.com/' rel='#myselector' class='ajax' /> 

它对href URL执行AJAX查询,并将结果注入由选择器在定位点rel属性中定义的第一个元素。

// Allow hrefs with the class 'ajax' and a rel attribute set with a selector to load content via ajax into the selected element. 
$('.ajax').unbind('click').click 
(
    function(e) 
    { 
     $($(this).attr('rel')).load($(this).attr("href")); 
     e.preventDefault(); 
    } 
); 
4

解释的页面,外部链接的绝对URL,并将它们设置在打开一个新标签,并具有友好的标题&类特殊造型。

$("#content [href^='http']:not(a:has('img'))").each(function(){$(this).attr("target", "_blank").addClass("external").attr("title", "External Link to " + $(this).attr("href"))}); 
0

扩展选择器,即编写自己的自定义选择器。下面是两个样本:

$(document).ready(function(){ 
    $.extend($.expr[':'], { 
     inputEmpty: inputEmpty, 
     inputNotEmpty: inputNotEmpty 
    }); 
}); 

function inputEmpty(el) { 
    return $(el).val() == ""; 
} 

function inputNotEmpty(el) { 
    return $(el).val() != ""; 
} 
相关问题