javascript - 如何用 Class CSS 覆盖内联 CSS?

标签 javascript jquery css

<分区>

我正在使用 jQuery 来附加一个元素,例如:

var mytop=100;
$(this).appendTo('.container')
       .last()
       .wrap('<div class="action" style="top:' + mytop + 'px;"></div>');

只有 mytop是一个计算出来的数字。由于新元素具有类 action , 它稍后应该通过添加类 moveup 来更改顶部,例如:

$('document').on('click', function(e){
   if($(e.target).hasClass('action')){
      $(this).addClass('moveup');
   }
});

CSS 为:

.moveup{
   top:5px;
}

问题是,附加的 <div> 的内联 CSS覆盖类的 CSS。单击这个新创建的元素时,它的顶部应该设置为 5px。

如何用类的 CSS 覆盖内联 CSS?

最佳答案

取自https://css-tricks.com/snippets/css/style-override-technique/

The !important rule at the end of a value will override any other style declarations of that attribute, including inline styles.

p { 
    font-size: 24px !important; 
}

关于javascript - 如何用 Class CSS 覆盖内联 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215630/

相关文章:

html - 如何在固定尺寸外部 div 中包含的内部 div 上获得垂直滚动条?

PHP 相当于 pythons 插槽

javascript - 我可以仅使用 JavaScript 发送电子邮件而无需单击电子邮件客户端上的“发送”按钮吗?

php - 动态更改 div 内容,同时页面的部分内容保持静态,并且还允许直接访问任何内容(想想书签)?

javascript - 点击infoBox不执行映射到 "click"函数的javascript

javascript - 使用 PapaParse 插件显示 CSV header

javascript - 与服务器通信的 html 小部件

javascript - Google map 地理编码始终使用浏览器语言

javascript - Angular : How to get the total for a shopping website

css - DIV 在 DIV 100% 高度内,古老的谜语