如何在javascript中更改按钮大小

如何在javascript中更改按钮大小,javascript,css,button,size,Javascript,Css,Button,Size,我正在使用javascript在chrome扩展中创建按钮,但我找不到改变按钮大小的方法,下面是代码 var buttonShort=document.createElement(“按钮”); buttonShort.innerHTML=“生成短密码”; var body=document.getElementsByTagName(“body”)[0]; 身体。附属物儿童(钮扣运动); buttonShort.addEventListener(“单击”,函数(){ var newWindow=

我正在使用javascript在chrome扩展中创建按钮,但我找不到改变按钮大小的方法,下面是代码

var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码”;
var body=document.getElementsByTagName(“body”)[0];
身体。附属物儿童(钮扣运动);
buttonShort.addEventListener(“单击”,函数(){
var newWindow=window.open();
newWindow.document.write(“生成的密码是:“+short()+””);
newWindow.focus()

});下面的代码如何

var cssString = "{padding: 15px 20px; color: #F00;}";
buttonShort.style.cssText = cssString;

下面的代码怎么样

var cssString = "{padding: 15px 20px; color: #F00;}";
buttonShort.style.cssText = cssString;

您可以使用
className
cssText
来执行此操作。 如果使用className,则需要在CSS中定义类

//通过设置css类名
var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码”;
//设置类名
buttonShort.className=“button”;
var body=document.getElementsByTagName(“body”)[0];
身体。附属物儿童(钮扣运动);
//或者使用JS
var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码2”;
//使用js设置CSS名称
buttonShort.style.cssText=“边框:1px纯黑色;背景色:蓝色;高度:20px;颜色:白色”;
var body=document.getElementsByTagName(“body”)[0];
身体。附属物儿童(钮扣运动)
。按钮{
边框:1px纯黑;
背景颜色:橙色;
高度:20px;

}
您可以使用
className
cssText
来执行此操作。 如果使用className,则需要在CSS中定义类

//通过设置css类名
var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码”;
//设置类名
buttonShort.className=“button”;
var body=document.getElementsByTagName(“body”)[0];
身体。附属物儿童(钮扣运动);
//或者使用JS
var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码2”;
//使用js设置CSS名称
buttonShort.style.cssText=“边框:1px纯黑色;背景色:蓝色;高度:20px;颜色:白色”;
var body=document.getElementsByTagName(“body”)[0];
身体。附属物儿童(钮扣运动)
。按钮{
边框:1px纯黑;
背景颜色:橙色;
高度:20px;

}
在将按钮元素附加到主体之前,请使用
style.width
属性,如下所示:

var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码”;
var body=document.getElementsByTagName(“body”)[0];
buttonShort.style.width='200px';//将宽度设置为200px
buttonShort.style.height='200px';//将高度设置为200px
buttonShort.style.background='teal';//将背景色设置为青色
buttonShort.style.color='白色';//将颜色设置为白色
buttonShort.style.fontSize='20px';//将字体大小设置为20px
身体。附属物儿童(钮扣运动);
addEventListener(“单击”,函数(){
var newWindow=window.open();
newWindow.document.write(“生成的密码是:“+short()+””);
newWindow.focus();

});在将按钮元素附加到主体之前,请使用
style.width
属性,如下所示:

var buttonShort=document.createElement(“按钮”);
buttonShort.innerHTML=“生成短密码”;
var body=document.getElementsByTagName(“body”)[0];
buttonShort.style.width='200px';//将宽度设置为200px
buttonShort.style.height='200px';//将高度设置为200px
buttonShort.style.background='teal';//将背景色设置为青色
buttonShort.style.color='白色';//将颜色设置为白色
buttonShort.style.fontSize='20px';//将字体大小设置为20px
身体。附属物儿童(钮扣运动);
addEventListener(“单击”,函数(){
var newWindow=window.open();
newWindow.document.write(“生成的密码是:“+short()+””);
newWindow.focus();

});我只需要改变一点,但尽管如此,它还是奏效了。谢谢我只是需要改变一点,但尽管如此,它还是奏效了。谢谢作为@tareq,如果您计划做的不仅仅是更改元素的宽度,而且不希望每次都执行此赋值,那么可以使用
style.cssText
属性并将其赋值给包含普通css的字符串。像这样:
button.style.cssText='width:20px;颜色:青绿色
As@tareq,如果您计划做的不仅仅是更改元素的宽度,而且不想每次都执行此赋值,则可以使用
style.cssText
属性并将其赋值给包含普通css的字符串。像这样:
button.style.cssText='width:20px;颜色:青绿色