简体   繁体   English

如何使用 .setattribute() 添加换行符?

[英]How to add a line break with .setattribute()?

I am a beginner programmer which have appended a list of buttons into a div .我是一名初学者程序员,已将按钮列表附加到div However, the list of buttons are side by side as shown below here,但是,按钮列表并排如下所示,
在此处输入图片说明
I want a line break after each button so they will be on top of each other instead.我想在每个按钮后换行,这样它们就会在彼此之上。 My codes to append using .setattribute() are as shown below,我使用.setattribute()追加的代码如下所示,

<script>

        var arr = [{"userid": "jim", "title":"help"}, {"userid": "pim", "title":"hello"}]  

        var i;
        $("enquirieswall").empty();
    for (i = 0; i < arr.length; i++) {
                          var a = document.createElement("button");
                          a.setAttribute("href", "#");
                          //a.textContent = arr[i].title;
                          a.innerHTML = arr[i].title.toString();
                          a.setAttribute("class", "btn");
                          a.setAttribute("type", "button");
                          a.setAttribute("onclick", "window.location='enquiriesdetails.php?enid=" + arr[i].enid + "&userid=" +arr[i].userid +"'");
                          $("#enquirieswall").append(a);
                        }
        </script>

        <div id="enquirieswall"></div>

Thanks guys, I picked the CSS solution in the end.谢谢大家,我最终选择了 CSS 解决方案。 I also aligned the buttons and set a fixed width.我还对齐按钮并设置了固定宽度。

<style>
.btn{
    display: block; 
    text-align:center;
    margin:auto;
    width: 80%;
}
</style>

end result:最终结果:
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM