Aquí traté de crear una función JS para crear botones, lo que hice con éxito. Lo que no me ha funcionado es encontrar una manera de mover/posicionar ese botón dentro de la misma función. Busqué en Internet una respuesta, pero no pude encontrar ninguna.
function createButtons(dialogueString, buttonIdString) { var btn = document.createElement("button"); btn.style.left = "50%"; //here is one of my attempts to move the button but it is not working btn.id = buttonIdString; btn.innerHTML = dialogueString; document.body.appendChild(btn); }; createButtons("Hello", "choice1"); Como puede ver arriba, intenté usar btn.style.left para mover el botón hacia la izquierda, intenté escribir "200px", en lugar de "50%" y muchas otras cosas, pero nada funcionó. Si tienes alguna idea de cómo solucionar esto, por favor házmelo saber, te lo agradecería mucho.
simplemente agregue btn.style.position = 'absolute'; o btn.style.position = 'relativ'; a tu funcion
function createButtons(dialogueString, buttonIdString) { var btn = document.createElement("button"); btn.style.left = "50%"; //here is one of my attempts to move the button but it is not working btn.style.position = 'absolute'; btn.id = buttonIdString; btn.innerHTML = dialogueString; document.body.appendChild(btn); }; createButtons("Hello", "choice1");Puede crear una clase css con el estilo deseado e incluirla en js.
Por ejemplo:
//style.css .buttonStyle{ color: red; } //javascript part btn.classList.add("buttonStyle");Encontré la solución. Hice btn.style.marginLeft = "+50px" y parece funcionar.