简体   繁体   English

使用Javascript函数更改CSS样式

[英]Change CSS style with Javascript function

I am trying to change a CSS style whenever a button is clicked. 每当单击按钮时,我都试图更改CSS样式。 To do this I have a Javascript function which defines a new class for CSS (as well as several other things). 为此,我有一个Javascript函数,它为CSS定义了一个新类(以及其他一些东西)。 Currently the JS function looks like this: 当前,JS函数如下所示:

scope.dropChange = function(dropValue, dataType) {
    scope.checkChangeCol = {
        'width': '8px;',
        'height': '20px;',
        'border': 'solid #000;',
        'border-width': '0 3px 3px 0;',
        'padding-left':'0px;',
        'margin-left':'13px;',
        '-moz-transform': 'rotate(45deg);',
        '-webkit-transform': 'rotate(45deg);',
        '-o-transform': 'rotate(45deg);',
        '-ms-transform': 'rotate(45deg);',
        'margin':'auto;'
}

*i took out a lot on unnecessary code. *我花了很多不必要的代码。 if you think the problem doesn't have to do with the class, can can add in the rest of the function. 如果您认为问题与类无关,则可以添加其余功能。

The html for the section that uses this is: 使用此部分的html是:

<table class=buttons-table>
    <tr style="line-height: 100px;">
        <td><button type="button" class="btn btn-primary btn-sm" ng-click="dropChange('all')">All</button>
        <td><button type="button" class="btn btn-primary btn-sm" ng-click="dropChange('room')">Room</button>
        <td><button type="button" class="btn btn-primary btn-sm" ng-click="dropChange('floor')">Floor</button>
        <td><button type="button" class="btn btn-primary btn-sm" ng-click="dropChange('building')">Building</button>

    </tr>

    <tr>
        <td><div style="{{checkChangeCol}}" class="checkmark"></div>
        <td><div class="checkmark"></div>
        <td><div class="checkmark"></div>
        <td><div class="checkmark"></div>

    </tr>

    <tr>
        <td>checkChangeCol = {{checkChangeCol}}
    </td>
</table>

From what i can tell, I believe i formatted the class within the function wrong, so it isn't being used even though the data is found. 据我所知,我相信我在函数中格式化了该类,因此即使找到了数据也没有使用它。 However, I am unsure of how to actually fix this. 但是,我不确定如何实际解决此问题。

为什么不使用简单的document.getElementById('yourid')。setAttribute('style','color:red')?

This can be done without using any jquery or JavaScript. 无需使用任何jquery或JavaScript即可完成此操作。 But you will need to restructure your HTML 但是您将需要重组HTML

<button class="btn">click me<span class="ouch"></span></button>

And then use the active psuedo class to add the effect 然后使用活动的psuedo类添加效果

.btn:active .ouch { color: red}

Further info https://css-tricks.com/pseudo-class-selectors/ 进一步的信息https://css-tricks.com/pseudo-class-selectors/

Why don't you use jQuery? 为什么不使用jQuery? http://jquery.com http://jquery.com

$(function(){
   $('button').click(function(){
      $('#attrib').css({
         color: 'red'
      });
   });
});

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

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