Javascript 如何将样式从一个按钮复制到另一个按钮?

Javascript 如何将样式从一个按钮复制到另一个按钮?,javascript,Javascript,我的目标是: 我试图用Javascript替换屏幕上的按钮 我需要按钮看起来相同,所以我尝试将样式从旧按钮复制到新按钮 问题是: 按钮样式没有反映在新按钮上,这可能是因为我没有正确地从oldButton获取它,或者因为我没有正确地将它应用到新按钮 这是我的密码: 有什么想法吗?谢谢 我不确定你到底想实现什么,但基于我需要按钮看起来一样,所以我尝试将样式从旧按钮复制到新按钮 这里有一个简单的解决方案 //编写Javascript代码! const appDiv=document.getEleme

我的目标是:

我试图用Javascript替换屏幕上的按钮

我需要按钮看起来相同,所以我尝试将样式从旧按钮复制到新按钮

问题是:

按钮样式没有反映在新按钮上,这可能是因为我没有正确地从oldButton获取它,或者因为我没有正确地将它应用到新按钮

这是我的密码:


有什么想法吗?谢谢

我不确定你到底想实现什么,但基于我需要按钮看起来一样,所以我尝试将样式从旧按钮复制到新按钮

这里有一个简单的解决方案

//编写Javascript代码! const appDiv=document.getElementByIdapp; constoldbutton=document.getElementById'button'; const oldButtonStyle=window.getComputedStyle document.getElementById'button1', //从伪选择器中提取样式的可选选项:before,:after .cssText; const newButton=document.createElementINPUT; newButton.style.cssText=oldButtonStyle; oldButton.parentNode.replaceChildnewButton,oldButton; h1, 氢{ 字体系列:Lato; } 钮扣{ 高度:64px; 边框:红色; 宽度:100px; 背景:红色; } 按钮1{ 高度:64px; 边框:红色; 宽度:100px; 背景:黄色; } 古老的 另一个按钮
我不确定你到底想实现什么,但基于我需要按钮看起来相同,所以我尝试将样式从旧按钮复制到新按钮

这里有一个简单的解决方案

//编写Javascript代码! const appDiv=document.getElementByIdapp; constoldbutton=document.getElementById'button'; const oldButtonStyle=window.getComputedStyle document.getElementById'button1', //从伪选择器中提取样式的可选选项:before,:after .cssText; const newButton=document.createElementINPUT; newButton.style.cssText=oldButtonStyle; oldButton.parentNode.replaceChildnewButton,oldButton; h1, 氢{ 字体系列:Lato; } 钮扣{ 高度:64px; 边框:红色; 宽度:100px; 背景:红色; } 按钮1{ 高度:64px; 边框:红色; 宽度:100px; 背景:黄色; } 古老的 另一个按钮
首先使用style.cssText获取元素样式

然后使用setAttribute设置样式

在下面的代码段中,您可以在单击id为btn2的按钮时设置其样式

const style=document.getElementByIdbtn1.style.cssText; 函数集样式{ getElementByIdbtn2.setAttribute'style',style; } 按钮1
按钮2首先使用style.cssText获取元素样式

然后使用setAttribute设置样式

在下面的代码段中,您可以在单击id为btn2的按钮时设置其样式

const style=document.getElementByIdbtn1.style.cssText; 函数集样式{ getElementByIdbtn2.setAttribute'style',style; } 按钮1
按钮2有什么想法吗?-也许吧,但首先。。。为什么?为什么要用文本输入替换按钮?你想用它来完成什么?只需将相同的类添加到“新建”按钮…有什么想法吗?-也许吧,但首先。。。为什么?为什么要用文本输入替换按钮?你试着用它来完成什么?只需将同一个类添加到新按钮…这个,做什么?为什么要添加button1?这是一个可选参数,用于从伪选择器中提取样式:before,:after。button1只是显示样式确实被复制了。注意:CSW中的背景色属性?为什么要添加button1?这是一个可选参数,用于从伪选择器中提取样式:before,:after。button1只是显示样式确实被复制了。注意:CSS中的背景色属性
oldButtonStyle = window.getComputedStyle(oldButton).cssText;

newButton = document.createElement("INPUT");
newButton.style.cssText = oldButtonStyle;

oldButton.parentNode.replaceChild(
  newButton,
  oldButton
);
Just use that old button color specification by using DOM in app.js file

    <body>
        <button onclick="myFunction()" id="btn" type="button" style="background-color: 
         green;cursor: pointer;">Click Me!</button>
        <script src='./app.js'></script>
    </body>


// create app.js file and clone the color by using id and create a new button tag and link whatever you provide specification to them and in lastjust append it

const colorElement = document.getElementById('btn').style.backgroundColor;

function myFunction() {
    var x = document.createElement("BUTTON");
    var t = document.createTextNode("Click me");
    x.style.backgroundColor = colorElement
    x.appendChild(t);
    document.body.appendChild(x);
}