Soy bastante nuevo en javascript. Estoy tratando de crear un sitio web donde puedas ingresar cuándo naciste y averiguar tu edad en días. Mi salida (variable) también está en JS, entonces, ¿cómo la importo y le doy estilo en CSS?
Aquí está el código Javascript:
function ageInDays() { // variables var birthYear = prompt("What Year Were You Born In?"); var ageInDayss = (2021 - birthYear) * 365; var textAnswerYikes = "Yikes... that's old" // variable CSS //text var h1 = document.createElement('h1'); var textAnswer = document.createTextNode("You are " + ageInDayss + " days old. " + textAnswerYikes) h1.setAttribute('id', 'ageInDays'); h1.appendChild(textAnswer); document.getElementById('flex-box-result').appendChild(h1); }Supongo que lo que significa su 'importación' es tener su h1 dentro del HTML. Si es así, simplemente puede agregar su h1 en el cuerpo HTML o cualquier otro elemento HTML que desee tener dentro (por ejemplo, div).
Su archivo JavaScript:
//text var h1 = document.createElement('h1'); // add inner text to your h1 using string template literal. h1.innerText = `You are ${ageInDayss} days old. ${textAnswerYikes}`; h1.setAttribute('id', 'ageInDays'); document.body.append(h1) // <- append your h1 into HTML body or other HTML element document.getElementById('flex-box-result').appendChild(h1);Para diseñar su h1, solo necesita seleccionar la ID que le asignó (ageInDays) y diseñarla en su archivo CSS.
Su archivo CSS:
#ageInDays { /* your CSS styling code goes here */ }Espero que esto responda a sus preguntas.