Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

353
Visualizações
Comprobar si un valor no se ha inicializado siempre devuelve que el valor no se ha inicializado, incluso si lo está.

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.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

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; }

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda