javascript - $(this).css 不改变 css 值

标签 javascript jquery css

我有这个代码

$('#downvotebutton').click(function() {
    $(this).css('background-color', 'red !important');
    console.log('foo');
    //more

当向下投票按钮被点击时,“foo”返回到控制台,但背景颜色没有改变。 jQuery 链接到页面的其他地方并在其他地方工作。可能是什么原因?

最佳答案

你真的需要 !important 吗?以下将起作用:

$(this).css('background-color', 'red');

或者如果你需要 !important:

$(this).css("cssText", "background-color: red !important;");

关于javascript - $(this).css 不改变 css 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22070397/

相关文章:

javascript - bxSlider 在 2 天前停止工作,没有代码更改

javascript - 如何在没有按钮的情况下使用 jQuery UI 图标?

javascript - html标签是否停止传播?

javascript - 通过单击框外部关闭模式

javascript - 在 jQuery 中,如果我不能使用 'this' ,如何从属性中获取 id ?

html - 将表格单元格的大小调整为零

JavaScript 和网站加载时间优化

javascript递归函数返回初始值

html - 为什么我的图像在大多数浏览器中都能正确定位,但在 IE 中却不能?

javascript - 重叠时如何滚动div