Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

367
Vistas
¿Cómo verificar si el elemento tiene algún atributo?

Estoy tratando de hacer que un verificador condicional verifique si el elemento tiene algún atributo o no.

var result = parentElm.hasAttribute(); Intenté usar esto, pero no estoy seguro de querer un atributo específico por nombre, solo quiero verificar si el elemento tiene algún atributo o atributo como longitud, tamaño o idk.

copyFormatCommand.js

 const model = this.editor.model; const selection = model.document.selection; const parentElm = selection.focus.parent; this.isEnabled = !!parentElm;

Aquí está el HTML que estoy usando para probar si hay varios elementos anidados y quiero verificar si el elemento principal de textNode tiene algún atributo HTML

 <p style="font: 11pt Times New Roman, Times, Serif; margin: 0;" xvid="c0fc3b7176d15af5d8c5c488a7fa675e"><span xvid="6f8db94eb5824fc90d39a2d9a127a98d">Annual Fund Operating Expenses</span></p> <p style="font: 11pt Times New Roman, Times, Serif; margin: 0;" xvid="c0fc3b7176d15af5d8c5c488a7fa675e"> <span xvid="6f8db94eb5824fc90d39a2d9a127a98d"><span xvid="1">hello</span></span> </p> <p>hello</p>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Puedes hacer algo como esto. Si el elemento tiene más de un atributo (ya que id ya es uno), devuelve verdadero, de lo contrario, es falso.

 function hasAttributes(id) { const attr = document.getElementById(id) if (attr.attributes.length > 1) { return true } else { return false } }
about 4 years ago · Juan Pablo Isaza Denunciar

0

El método Object.keys() devuelve una matriz de los nombres de propiedad enumerables propios de un objeto dado, iterados en el mismo orden que lo haría un bucle normal.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede usar la propiedad element.attributes para obtener atributos. MDN tiene un ejemplo https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes#enumerating_elements_attributes

 function listAttributes() { var element = document.getElementById("myElementId"); var result = document.getElementById("result"); // First, let's verify that the paragraph has some attributes if (element.hasAttributes()) { var attrs = element.attributes; var output = ""; for(var i = attrs.length - 1; i >= 0; i--) { output += attrs[i].name + "->" + attrs[i].value; } result.value = output; } else { result.value = "No attributes to show"; } }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda