Estoy intentando un script similar a jQuery que admita selectores, mostrar, ocultar, texto y métodos html. Al ejecutar una prueba, independientemente del método al que llame, siempre arroja un error personalizado "No se seleccionó ningún elemento". ¿Cómo puedo solucionar esto? El código completo está aquí --> https://jsfiddle.net/7ado13vg/
El código relevante está debajo
var storingAttributes = {}; class MONEY { constructer(string) { try { //check for action if (string != null) { //Check Selection Type storingAttributes.selectorArray = string.split('') if (storingAttributes.selectorArray[0] == '#') { storingAttributes.selectionType = 'id'; storingAttributes.selectorArray.splice(0, 1); } if (storingAttributes.selectorArray[0] == '.') { storingAttributes.selectionType = 'class'; storingAttributes.selectorArray.splice(0, 1); } if (storingAttributes.selectorArray[0] != '#' && storingAttributes.selectorArray[0] != '.') { storingAttributes.selectionType = 'tag'; } //Select Elem if (storingAttributes.selectionType == 'id') { storingAttributes.selectedElem = document.querySelector(string); } if (storingAttributes.selectionType == 'class') { storingAttributes.selectedElem = document.querySelectorAll(string); } if (storingAttributes.selectionType == 'tag') { storingAttributes.selectedElem = document.querySelectorAll(string); } } } catch (error) { } } //The reason I checked what was being selected is because querySelectorAll returns an array, and I do not believe that ids are supported //trying to use my method 'show' show() { try { if (typeof storingAttributes.selectedElem == 'undefined' || storingAttributes.selectedElem == null) { throw "No Element Selected"; } else { if (storingAttributes.selectionType == 'tag') { for (n = 0; n < storingAttributes.selectedElem.length; ++n) { storingAttributes.selectedElem[n].style.display = 'block'; } return; } if (storingAttributes.selectionType == 'class') { for (n = 0; n < storingAttributes.selectedElem.length; ++n) { storingAttributes.selectedElem[n].style.display = 'block'; } return; } if (storingAttributes.selectionType == 'id') { storingAttributes.selectedElem.style.display = 'block'; return; } } } catch (e) { console.log(e); } } } Como puede ver, el constructor simplemente asigna/inicializa un valor para selectionType , selectedElem y selectorArray en el objeto storingAttributes
Cuando registras storingAttributes de almacenamiento, devuelve un objeto vacío.
Lo siguiente reemplaza el código que ha publicado para mostrar cuánto más simple se puede escribir y sin usar erróneamente try/catch .
Entiendo que no responde a su pregunta, pero el código con el que está trabajando es tan innecesariamente excesivo que es difícil filtrarlo para encontrar su problema sin refactorizarlo. El código más simple es más fácil de depurar.
var storingAttributes = {}; // Just test for the non-existance of a value if (!storingAttributes.selectedElem) { throw "No Element Selected"; } else { // switch is more concise when you have a single value to // check against multiple possible values switch (storingAttributes.selectionType){ case "tag": // And since you want to do the exact same code if // it's "tag" or "class", we'll allow fall through here case "class": // The Array.prototype.forEach method makes looping much simpler storingAttributes.selectedElem.forEach(function(item){ item.classList.add("block"); }); break; case "id": storingAttributes.selectedElem.classList.add("block"); } } /* Avoid inline styles which lead to duplication of code. Instead, use CSS classes where possible. */ .block { display:block; }