Created
March 22, 2016 23:03
-
-
Save mmfilesi/50de66744914f4a94d57 to your computer and use it in GitHub Desktop.
jquery style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
(function () { | |
var foo = function (params) { | |
return new DomModule(params); | |
}; | |
var DomModule = function (params) { | |
var selector = document.querySelectorAll(params), | |
i = 0; | |
this.length = selector.length; | |
for (; i < this.length; i++) { | |
this[i] = selector[i]; | |
} | |
return this; | |
}; | |
var methods = {}; | |
methods.addClass = function(classAdd) { | |
var len = this.length; | |
if ( classAdd ) { | |
while (len--) { | |
this[len].classList.add(classAdd); | |
} | |
} | |
return this; | |
}; | |
methods.removeClass = function(classRemove) { | |
var len = this.length; | |
if ( classRemove ) { | |
while (len--) { | |
this[len].classList.remove(classRemove); | |
} | |
} | |
return this; | |
}; | |
methods.toggleClass = function(classToggle) { | |
var len = this.length; | |
if (classToggle ) { | |
while (len--) { | |
this[len].classList.toggle(classToggle); | |
} | |
} | |
return this; | |
}; | |
methods.setStyle = function(style, value) { | |
var len = this.length; | |
if ( style && value ) { | |
while (len--) { | |
this[len].style[style] = value; | |
} | |
} | |
return this; | |
}; | |
methods.text = function(text) { | |
var len = this.length; | |
while (len--) { | |
this[len].textContent = text; | |
} | |
return this; | |
}; | |
methods.html = function(content) { | |
var len = this.length; | |
if ( content.indexOf('script') !== -1 ) { | |
content = 'scripts are not allowed'; | |
} | |
while (len--) { | |
this[len].innerHTML = content; | |
} | |
return this; | |
}; | |
methods.remove = function() { | |
var len = this.length; | |
while (len--) { | |
if ( this[len].parentNode ) { | |
this[len].parentNode.removeChild(this[len]); | |
} | |
} | |
return true; | |
}; | |
foo.fn = ArrakisDomModule.prototype = methods; | |
if ( !window.foo ) { | |
window.foo = foo; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment