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

181
Visualizações
Get all set css Properties of an element via Javascript

I'm currently workin on a HTML Designer (https://github.com/node-projects/web-component-designer). For this in need to read all set css properties of an object. At the moment I use this code:

window.onload = function(){
  let node = document.querySelector('b-c');
  for (let s of node.style) {
    let val = node.style[s];
    console.log(s, val)
    //if (val && typeof val === 'string')
      //designItem.styles.set(s, node.style[s]);
  }
}
<b-c style="background: var(--kx-dark)"></b-c>

The problem is, with the for loop I do not get all styles. For example, you don't get the "background" in the loop

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

To get --kx-dark you can read the attribute of the tag

Also the var is visible when getting the background which is a grouped style not showing as .style[..]

Note the loop does not resolve the computedStyle so no background-color either from the loop - here are a few workarounds

document.querySelectorAll("b-c").forEach((elem, i) => console.log(i, elem.getAttribute("style")))


//getComputedStyle does not show --kx-dark

document.querySelectorAll("b-c").forEach((elem, i) => console.log(i, window.getComputedStyle(elem).backgroundColor))

// let's try this:

window.onload = function() {
  const styleDict = {}
  let nodes = document.querySelectorAll('b-c');
  nodes.forEach((node,i) => {
    for (let s of node.style) {
      styleDict[s] = node.style[s];
      if (s.indexOf("-") != -1) { // get the bundle
        const mainStyle = s.split("-")[0]
        styleDict[mainStyle] = node.style[mainStyle];
      }
    }
    console.log(i,styleDict)
  })
}
:root {
  --kx-dark: rgb(165, 50, 50);
}
<b-c style="background: var(--kx-dark)">Is this brown?</b-c>

<b-c style="background-color: var(--kx-dark)">Is this brown?</b-c>

about 4 years ago · Juan Pablo Isaza Relatório

0

The reason you don't see the background style is because there is no such thing as a background style. To be more clear, while there is a background style, it's actually a collection of other styles. What you see in a browser's developers tool is actually a shorthand of several background styles that are chained in the single background that are later parsed into their specific attributes.

Look at your code

let node = document.querySelector('b-c');
for (let s of node.style) {
    let val = node.style[s];
    console.log(s, val)
}
<b-c style="background: var(--kx-dark);color:red;"></b-c>

You can see the color style as well as all the properties extracted from the background value.

Now look at this code

let node = document.querySelector('b-c');
  for (let s of node.style) {
    let val = node.style[s];
    console.log(s, val)
}
<b-c style="background-color: var(--kx-dark);color:red;"></b-c>

You get only two values, because there is only one specific background value.

about 4 years ago · Juan Pablo Isaza Relatório

0

let elem = document.querySelector("#title");

function getAllStyleProperties(elem){
    let allStyle = {};
    if(elem){
        let styleText = elem.style.cssText;
        let allStyleText = styleText.split(";");
        allStyleText.forEach((style) => {
            if(style){
                let [properties, value] = style.split(":");
                allStyle[properties.trim()] = value.trim();
            }
        })
    }
    return allStyle;
}

console.log(getAllStyleProperties(elem))
<h1 style="background: var(--kx-dark); margin: 10px" id="title">Hello World</h1>

You can try this...

about 4 years ago · Juan Pablo Isaza 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