简体   繁体   English

如何使用JavaScript创建中心按钮?

[英]How to create center buttons using JavaScript?

I know my issue is easy to solve when using HTML, but here is how my buttons are set: 我知道我的问题在使用HTML时很容易解决,但是这是我的按钮设置方式:

(In a JavaScript file) (在JavaScript文件中)

var myButton=$('<button type="button" style="display:inline">Click</button>');

I have 6 buttons, and I would like to center all the buttons (they are put horizontally). 我有6个按钮,我想将所有按钮居中(它们水平放置)。

I tried to assign a class to each button, then wrap them in a same div : 我试图为每个按钮分配一个类,然后将它们包装在同一个div

$('.class1, .class2, .class3').wrapAll('<div class="wrap">');

And put in my CSS file 并放入我的CSS文件

.wrap {
   text-align: center;
    }

But it didn't work. 但这没有用。

Actually, I don't need class1 , class2 . 实际上,我不需要class1class2 Is there any other way to wrap all the buttons in a same div ? 还有其他方法可以将所有按钮包装在同一个div吗?

Well you made them inline....They are doing what you told them to do, be in one line. 好吧,您使他们成为了内联...。他们正在按照您的指示去做,排成一排。

Make their display as block if you want them on their own lines. 如果您希望它们自己display ,则将其displayblock

将它们设置为是inline-block和所述设定width包裹物到它的父的宽度。

Set width and margin to the wrap 设置宽度和边距

.wrap{
   width:400px; 
   margin:0 auto;  
}

Hope this helps http://jsfiddle.net/ZHdqK/2/ 希望这对http://jsfiddle.net/ZHdqK/2/有帮助

$('button').wrapAll($('<div class = "wrap">'));

.wrap{
    text-align:center;  
}

http://jsfiddle.net/rJD2B/ http://jsfiddle.net/rJD2B/

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

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