![](/img/trans.png)
[英]How to set css style to local storage for dynamically created list using Javascript?
[英]Javascript: How to center multiple buttons that were created in Javascript using dynamically set CSS Style
我正在嘗試使用動態設置的 CSS 樣式在 Javascript 中創建一行三個按鈕,但我在嘗試將按鈕行居中放置在頁面中間時遇到困難。 這是當前不屬於 div 的一部分的按鈕。
我試過button.align = 'center'; 沒有成功。
這是jsfiddle片段的鏈接。
HTML 骨架
<head>
<meta charset="utf-8">
<title>Buttons</title>
</head>
<body>
<script>
</script>
</body>
</html>
爪哇腳本
var one, two, three;
function button(text) {
var button = document.createElement('button');
var buttonText = document.createTextNode(text);
button.appendChild(buttonText);
return button;
}
function buttonTest() {
one = button('one');
two = button('two');
three = button('three');
// put the buttons on page
document.body.appendChild(one);
document.body.appendChild(two);
document.body.appendChild(three);
}
buttonTest();
var one, two, three;
function button(text) {
var button = document.createElement('button');
var buttonText = document.createTextNode(text);
button.appendChild(buttonText);
return button;
}
function buttonTest() {
one = button('one');
two = button('two');
three = button('three');
var divElem = document.createElement('div');
divElem.setAttribute('style', 'text-align:center;');
// put the buttons on page
//document.body.appendChild(one);
//document.body.appendChild(two);
//document.body.appendChild(three);
divElem.appendChild(one);
divElem.appendChild(two);
divElem.appendChild(three);
document.body.appendChild(divElem);
}
buttonTest();
一個快速的解決方案是將text-align : center
添加到body
var one, two, three; function button(text) { var button = document.createElement('button'); var buttonText = document.createTextNode(text); button.appendChild(buttonText); return button; } function buttonTest() { one = button('one'); two = button('two'); three = button('three'); // put the buttons on page document.body.appendChild(one); document.body.appendChild(two); document.body.appendChild(three); } buttonTest();
body {text-align: center;}
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Buttons</title> </head> <body> <script> </script> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.