简体   繁体   English

为什么我不能在jQuery中使用.css()更改行高CSS属性?

[英]Why can't I change the line-height CSS property using .css() in jQuery?

I'm trying to change the line-height property with jQuery using this: 我正在尝试使用以下命令更改jQuery的line-height属性:

jQuery(document).ready(function () {
    var windowWidth = $(window).width();
    $('h1').css({
        'top': Math.floor(windowWidth * 0.0964) + 'px',
        'left': Math.floor(windowWidth * 0.0872) + 'px',
        'line-height': '0.9em !important'
    });
});

If I use the inspector and check the element style, I can see the top and left property but not the line-height, can someone explain to me what's wrong? 如果我使用检查器并检查元素样式,我可以看到顶部和左侧属性而不是行高,有人可以向我解释什么是错的吗?

And here's the jsFiddle I made just to be sure I'm not crazy! 这就是我做的jsFiddle,以确保我不会疯狂!
http://jsfiddle.net/p4DU6/4/ http://jsfiddle.net/p4DU6/4/

Tested it in FF, Chrome, IE8 and IE9 在FF,Chrome,IE8和IE9中测试过

Don't use !important . 不要使用!important There's no need for that. 没有必要这样做。

It works for me if you remove that. 如果删除它,它对我有用。

Your inline style will have higher specificity than anything else in your CSS anyway, so no need to use !important . 无论如何,你的内联样式将具有比CSS中任何其他内容更高的特异性 ,因此不需要使用!important

This is discussed here: How to apply !important using .css()? 这里讨论: 如何申请!重要使用.css()?

The problem seems to be the !important statement. 问题似乎是!重要的声明。 There is a bug report for jquery and a way to work around it, namely creating new classes and combining them: 有一个关于jquery的bug报告以及解决它的方法,即创建新类并组合它们:

http://bugs.jquery.com/ticket/11173 http://bugs.jquery.com/ticket/11173

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

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