Guest User

Untitled

a guest
Nov 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. Element.prototype.getAncestorByClassName = function(className){
  2. var el = this;
  3. while ((el = el.parentNode)) {
  4. if(typeof el.className === "string" && el.className.match(className)) {
  5. return el;
  6. }
  7. }
  8. }
  9.  
  10.  
  11. Element.prototype.getClass = function(className){
  12. if(this.nodeName.match(/svg|circle/)) {
  13. return this.className.baseVal;
  14. }
  15. return this.className;
  16. }
  17.  
  18. Element.prototype.hasClass = function(className){
  19. return this.getClass().match(className);
  20. }
  21.  
  22. Element.prototype.addClass = function(className){
  23. if(this.hasClass(className)) {
  24. return;
  25. }
  26. this.className += " " + className;
  27. }
  28.  
  29. Element.prototype.removeClass = function(className){
  30. this.className = this.className.replace(className, '');
  31. }
  32.  
  33. Element.prototype.replaceClass = function(oldClassName, newClassName){
  34. this.className = this.className.replace(oldClassName, newClassName);
  35. }
  36.  
  37. Element.prototype.hide = function(){
  38. this.style.display = "none";
  39. }
  40.  
  41. Element.prototype.show = function(displayStyle){
  42. if(!displayStyle) {
  43. displayStyle = 'block';
  44. }
  45. this.style.display = displayStyle;
  46. }
  47.  
  48. Element.prototype.replaceOrAddClass = function(oldClassName, newClassName){
  49. this.replaceClass(oldClassName, newClassName);
  50. this.addClass(newClassName);
  51. }
  52.  
  53. if (typeof HTMLDocument !== 'undefined') {
  54. HTMLDocument.prototype.getElementByClassName = getElementByClassName;
  55. } else {
  56. Document.prototype.getElementByClassName = getElementByClassName;
  57. }
  58.  
  59. Element.prototype.getChildByClassName = function(className){
  60. var children = this.children || this.childNodes;
  61. for (var i = 0; i < children.length; i++) {
  62. if (children[i].nodeType == 3) {
  63. continue;
  64. }
  65. var child = children[i];
  66. if (child.hasClass(className)) {
  67. return children[i];
  68. }
  69. var found = child.getChildByClassName(className);
  70. if(found) {
  71. return found;
  72. }
  73. }
  74. return;
  75. }
  76.  
  77. String.prototype.toUpperCaseWord = function(){
  78. return this.replace(/\b[a-z]/g, function(letter) {
  79. return letter.toUpperCase();
  80. });
  81. }
Add Comment
Please, Sign In to add comment