jQuery .css 不适用于父容器

标签 jquery css

<分区>

我正在尝试使用以下语句应用内联 CSS。

$('.WarningCount').parent().css('margin-left', '-1.4% !important;');

但它不起作用,它不只是被应用。我在开发人员控制台中看不到这种样式。我必须覆盖整个样式才能使其正常工作。

$('.WarningCount').parent().attr('style', 'margin-left:-1.4% !important;');

任何可能是这里真正问题的原因。

最佳答案

取自jQuery documentation :

Note: .css() ignores !important declarations. So, the statement $( "p" ).css( "color", "red !important" ) does not turn the color of all paragraphs in the page to red. It's strongly advised to use classes instead; otherwise use a jQuery plugin.

虽然,你可以引用this answer并使用 cssText 属性

const target = $('.WarningCount').parent();
target.css('cssText', target.attr('style')+'margin-left: -1.4% !important;');

关于jQuery .css 不适用于父容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52002012/

上一篇:HTML/CSS 嵌套左边框 : reiterative visual effect

下一篇:html - CSS:由于 css 链接,php 弄乱了模板 css

相关文章:

jquery - 如何抑制 jquery 验证取消?

javascript - 解析器错误/语法错误 : Unexpected token <

html - IE7 CSS 填充问题 - 无法弄清楚

html - 我想让这个选择标签响应,同时保持对齐

css -::first-letter 与 :first-child 有何不同(伪类与伪元素)

javascript - Onfocus 文本框用户名放入文本框名字值和文本框姓氏值分割

javascript - 为什么我没有在 Bootstrap 中获得所需的导航栏?

javascript - 禁用页面中的所有 kendoui 元素

html - 垂直居中 Font Awesome 图标

javascript - 是否可以用 JS/CSS 反转 png?