Skip to content

Instantly share code, notes, and snippets.

@kotarok
kotarok / RegEx enabled jQuery.removeClass
Created June 24, 2009 08:20
be able to remove class by RegExp
(function($){
$.fn._removeClass = $.fn.removeClass
$.fn.removeClass = function(arg) {
return this.each(function(){
if(typeof(arg.test) == 'function'){
var classes = $(this).attr('class').split(/\s/);
$.each(classes,function(i){if(arg.test(this)){classes.splice(i,1)}});
$(this).attr('class',classes.join(' '));
}else{
$(this)._removeClass(arg);
@kotarok
kotarok / jQuery.replaceClass by RegEx
Created June 24, 2009 08:21
replace class by RegExp
(function($){
$.fn.replaceClass = function(RE,STR) {
return this.each(function(){
var classes = $(this).attr('class').split(/\s/);
$.each(classes,function(i){classes[i] = this.replace(RE,STR)})
$(this).attr('class',classes.join(' '));
})
};
})(jQuery)
@kotarok
kotarok / gist:299050
Created February 9, 2010 09:32
mark part of string with given jQuery element by RegExp
/**
* @param query {RegExp} required
* @param wrapElement {jQuery} required
**/
$.fn.markTextByRegExp = function(query,wrapElement){
var REtag = '<\\/?[^>]+?\\/?>';
var REtagDivider = new RegExp(REtag+'|[^<>]*','gi');
return this.each(function(){
var elem = $(this);
@kotarok
kotarok / jQuery.lang.js
Created February 9, 2010 09:33
get the language of target element
/**
* @param langid {String} optional
* @return {jQuery} when langid is given.
* @return langid {String} when langid is not given.
**/
$.fn.lang = function(langid) {
var elem = $(this);
var lang;
/**
* $.require('foo.js');
* $.require(['foo.js','baa.js','moo/muu.js]);
*/
jQuery.require= (function(){
var jspath = $('script[src]:last').attr('src');
var dirpath = /.+\//.exec(jspath)[0]
return function(jsfiles){
@kotarok
kotarok / my clearfix
Created November 28, 2010 10:28
the clearfix I'm currently using
.clearfix {
*zoom: 1;
}
.clearfix:after {
content: '';
display: block;
clear : both;
height: 0;
}
@kotarok
kotarok / give 'first-child' class in
Created December 4, 2010 00:37
In this gist, add 'first-child' class for first-child element. This runs expression only one time. So browser doesn't get slow.
.someClass{
behavior: expression(this.style.behavior || ((function(a,b,c){
c=a.parentNode.getElementsByTagName('!');
try{for(var i=0,l=c.length;i<l;i++){c[i].parentNode.removeChild(c[i])}}catch(e){};
if(a.parentNode.firstChild==a){a.className+=' '+b}
})(this,"lu.first-child"),1));
}
.whatever slector {
/*run behaviour only one time*/
behavior: expression(this.behavior || (function(that){try{
/*do something here*/
}catch(e){};})(this),1);
}
html {
/*to remove all comment nodes on IE*/
behavior: expression(this.behavior || ((function(a){
a=document.getElementsByTagName('!');
try{for(var i=0,l=a.length;i<l;i++){a[i].parentNode.removeChild(a[i])}}catch(e){};
})(),1));
}
@kotarok
kotarok / jquery.stickyfooter.js
Created January 16, 2011 12:22
Make footer stick to the bottom edge. Cares not to overlap upper content. Make sure that base ancestor element's position is 'relative.' I believe that mostly its 'body' element.
/*!
* jquery.stickyfooter.js
*
* Copyright 2011, Kotaro Kokubo
* kotaro@nodot.jp
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
// In jQuery1.4.4, .outerHeight() or .height doesn't work collecly sometimes.
$.fn.stickyfooter = function(){