简体   繁体   English

如何使用JavaScript动态更改CSS Bootstrap样式?

[英]How to change CSS Bootstrap style using JavaScript dynamically?

I have this style in my custom css stylesheet: 我的自定义CSS样式表中有以下样式:

.nav.nav-tabs > li > a {
    background-color: #c6dfff;
    margin-right: 5px;
    padding-left: 20px;
    padding-right: 20px;
    cursor: pointer;
    color: black;
}

I have a button using AngularJS ng-click: 我有一个使用AngularJS的按钮ng-click:

<button type="button" class="btn btn-primary" ng-click="changeColor()">Change Color</button>

When the user clicks on the button, it calls a changeColor method: 当用户单击按钮时,它将调用changeColor方法:

            $scope.changeColor= function () {
                // Change Color Here
            }; 

I want to dynamically change the background-color of the above listed style to a different color. 我想将上述样式的背景颜色动态更改为其他颜色。

alright, so If you want to use javascript, you could do this one of two ways. 好吧,因此,如果您想使用javascript,则可以采用以下两种方法之一进行操作。 The first would be to assign the object an ID, or if multiple objects you would use a class. 第一种是为对象分配一个ID,或者如果多个对象将使用一个类。

for an ID 为一个ID

document.getElementById("myId").style.backgroundColor="yourcolor"

replace your color with the color you want and myID with the ID of the object. 将颜色替换为所需的颜色,将myID替换为对象的ID。 If you have multiple objects with the same class, 如果您有多个具有相同类别的对象,

for a class 一堂课

document.getElementsByClassName("myclassname").style.backgroundColor="yourcolor"

again, replace the myclassname with the name of the class; 再次,将myclassname替换为类的名称; same for your color 同样的颜色

or if you want to use JQuery 或者如果您想使用JQuery

$(".myclassname").css("background-color", "your color");

typically, you use classes for multiple items, and IDs for single ones. 通常,您将类别用于多个项目,将ID用于单个项目。 feel free to comment and Ill add anything else you need; 随时发表评论,我会添加您需要的其他任何内容; as i am a little uncertain of the specifics of what you are asking. 因为我不确定您要问的细节。

Get the selected value from dropdown, either by using onchange on the select or in a button click. 通过使用select上的onchange或单击按钮,从下拉列表中获取选定的值。 Then use the value to assign it to the background of the element you want. 然后使用该值将其分配给所需元素的背景。

HTML: HTML:

<select id="bgselect" onchange="changeBg()">
    <option value="black">Black</option>
    <option value="blue">Blue</option>
    <option value="#000080">Navy Blue</option>
    ...
</select>

Javascript: 使用Javascript:

function changeBg() {
    var bg = document.getElementById("bgselect").value;
    document.getElementById("dbg").style.background = bg;
}

jsfiddle DEMO jsfiddle演示

EDIT: 编辑:

If you wanna affect elements like .nav.nav-tabs > li > a then you need to find all such elements and then apply the style: 如果要影响.nav.nav-tabs > li > a类的元素,则需要找到所有此类元素,然后应用样式:

function changeBg() {
    var bg = document.getElementById("bgselect").value;
    var navs = document.querySelectorAll(".nav.nav-tabs");
    var as = navs[0].getElementsByTagName('a');
    for(var i = 0; i < as.length; i++) {
        as[i].style.background = bg;
    }
}

updated jsfiddle 更新了jsfiddle

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

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