简体   繁体   English

将CSS样式应用于Javascript中的按钮

[英]Applying CSS style to a button in Javascript

I need to apply CSS style defined in main.css to a button from a handler class 我需要将main.css中定义的CSS样式应用于处理程序类的按钮

In JavaScript, I am hiding/displaying a button using eventContext.setDisplay(true/false) 在JavaScript中,我使用eventContext.setDisplay(true / false)隐藏/显示按钮

I need to apply CSS style to that button when setDisplay is set to true. 当setDisplay设置为true时,我需要将CSS样式应用于该按钮。 The button is visible but not having required style. 该按钮可见,但没有必需的样式。

app.xml app.xml

 <button id="btn1" label="Done"> <eventHandlers id="btn1_eventHandlers"> <eventHandler class="custom.handlers.MyHandler" event="render" id="button1_eventHandlers_render_commitEntry" method="done"/> </eventHandlers> </button> 

done method is defined in MyHandler.js 完好的方法在MyHandler.js中定义

    done:  function(eventContext) {
        if (count==10)
        {
            eventContext.setDisplay(true);
        }
    }

The button gets displayed on the device. 该按钮将显示在设备上。 Is there any way to apply CSS style to this button at this point so that it can be displayed in intended format? 目前有什么方法可以将CSS样式应用于此按钮,以便可以按预期格式显示它?

Simple: 简单:

document.getElementByID('yourThing').style.property = 'whatever';

More info here: http://www.w3schools.com/js/js_htmldom_css.asp 此处提供更多信息: http : //www.w3schools.com/js/js_htmldom_css.asp

If you already have a class containing the button's style, just select your button and add this css class to it 如果您已经有一个包含按钮样式的类,只需选择您的按钮并将此CSS类添加到其中

    document.getElementById("button1_eventHandlers_render_commitEntry").className = "your_class_name";

If you don't have a css file containing this class, you can apply the style this way. 如果没有包含此类的css文件,则可以这种方式应用样式。 Select your element and use the style property, and set your css formatting. 选择您的元素并使用style属性,然后设置CSS格式。

    document.getElementById("elementId").style.color = "red";

    // To use properties with two words like "background-color" use this format "backgroundColor"

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

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