gpt4 book ai didi

javascript - 如何在 javascript 中更改按钮大小

转载 作者:行者123 更新时间:2023-11-30 14:55:21 31 4
gpt4 key购买 nike

我正在使用 javascript 在 chrome 扩展程序中创建按钮,但我找不到更改按钮大小的方法,这是代码。

var buttonShort = document.createElement("button");
buttonShort.innerHTML = "Generate Short Password";

var body = document.getElementsByTagName("body")[0];
body.appendChild(buttonShort);

buttonShort.addEventListener ("click", function() {
var newWindow = window.open();
newWindow.document.write("The generated Password is: '" + short() + "'");
newWindow.focus()
});

我需要更改按钮的大小(主要是宽度),所以如果您有任何建议,请说。此外,如果您要说使用 CSS,我不知道如何将 CSS 文件添加到 javascript 文件,所以如果您不介意,请告诉我该怎么做。

谢谢。

最佳答案

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

var buttonShort = document.createElement("button");
buttonShort.innerHTML = "Generate Short Password";

var body = document.getElementsByTagName("body")[0];

buttonShort.style.width = '200px'; // setting the width to 200px
buttonShort.style.height = '200px'; // setting the height to 200px
buttonShort.style.background = 'teal'; // setting the background color to teal
buttonShort.style.color = 'white'; // setting the color to white
buttonShort.style.fontSize = '20px'; // setting the font size to 20px

body.appendChild(buttonShort);

buttonShort.addEventListener("click", function() {
var newWindow = window.open();
newWindow.document.write("The generated Password is: '" + short() + "'");
newWindow.focus();
});

所有其他 CSS 属性都可以通过 style 对象访问(宽度、高度、颜色等)

NOTE: beware of properties like font-size you cannot use the - as an object key so the way you would go about doing that is by camelCasing it like so fontSize.

关于javascript - 如何在 javascript 中更改按钮大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383288/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com