简体   繁体   English

尝试将“!重要”附加到使用 JS 创建的内联样式

[英]Trying to append "!important" to an inline style created with JS

I'm working on an element that I need to be able to add '!important' to my inline style that I am dynamically creating using JS in order to override several CSS styles that exist in stylesheets developed by another person.我正在处理一个元素,我需要能够将 '!important' 添加到我使用 JS 动态创建的内联样式中,以便覆盖其他人开发的样式表中存在的多个 CSS 样式。 Unfortunately, these stylesheets are used globally and this particular area has a slew of '!important's strung about through it that I cannot modify without risking altering previous headers that exist throughout the site.不幸的是,这些样式表在全球范围内使用,并且这个特定区域有大量的 '!important 字符串通过它我无法修改,而不会冒改变整个站点存在的先前标题的风险。 I'm fairly rough in my JS skills, thus am having a rough go of figure this out.我的 JS 技能相当粗糙,因此我很难弄清楚这一点。

Here is my current code snippet:这是我当前的代码片段:

fixTextPosition: function()
{
  var width = $( window ).width();
    if (width > 1451){
      width = 1451;
    }
  var height = $( window ).height();
    if (height > 816){
      height = 816;
    }
  $("#banner-resize").css("minheight", height);
    if (height < 816){
      $("#banner-resize").css("minHeight", height - 1);
    }
}

I am attempting to append "!important" to the "minHeight" value after it has gone through (height - 1) and cannot seem to figure out how to add the value as a string.我试图在“minHeight”值经过(高度 - 1)后将“!important”附加到它,但似乎无法弄清楚如何将该值添加为字符串。

Thank you in advance to any helpers!在此先感谢任何帮助者!


Edited After Answers回答后编辑

Thanks to all who gave some thoughts - I'll take what I learned from posting my question in here to make sure my next question is better phrased and given with more context.感谢所有提出一些想法的人 - 我将从在这里发布我的问题中吸取教训,以确保我的下一个问题措辞更好,并提供更多上下文。 :) :)

The above question is referring to a global header element that has several "min-height = x!important" existing in global stylesheets that a previous developer created - meaning I don't get to play as freely as I would like.上面的问题指的是一个全局标题元素,该元素在以前的开发人员创建的全局样式表中存在多个“min-height = x!important”——这意味着我不能像我想的那样自由地玩。 I was able to work around it by creating a new class that contains all of the previous positioning styles and the new "min-height=x" sans '!important' which allowed me to use the above JS as desired.我能够通过创建一个包含所有以前定位样式的新类和新的“min-height=x”sans '!important' 来解决它,这允许我根据需要使用上述 JS。

I'm still curious to see if there is a more simple JS workaround to append an '!important' to an inline style as I don't really want to be creating new classes every time I run into this particular situation.我仍然很想知道是否有更简单的 JS 解决方法来将 '!important' 附加到内联样式,因为我真的不想每次遇到这种特殊情况时都创建新类。

If you set any CSS style by jQuery, this style is added to DOM element style attribute. 如果您通过jQuery设置任何CSS样式,则此样式将添加到DOM元素样式属性。

For example, if you have element: 例如,如果您具有元素:

<p class="test" id="testId">

and you will add style by jQuery: 然后您将通过jQuery添加样式:

$(".test").css("min-height", 30);

In your DOM you will now have: 在您的DOM中,您现在将拥有:

<p class="test" id="testId" style="min-height: 30px">

And styles from style attribute overwrite styles from included CSS file, unless you use !impotrant in CSS file. 除非您在CSS文件中使用!impotrant,否则来自样式属性的样式会覆盖包含的CSS文件中的样式。

Just don't use !important in CSS. 只是不要在CSS中使用!important。 And when you set new style value by jQuery it will automaticly overwrite style value. 并且当您通过jQuery设置新样式值时,它将自动覆盖样式值。


How to set min-height style attribute without jQuery: 如何在不使用jQuery的情况下设置最小高度样式属性:

by 'id' attribute: 通过“ id”属性:

document.getElementById("testId").style.minHeight = "30px";

by 'class' attribute: 通过“ class”属性:

document.querySelector(".test").style.minHeight = "30px";

querySelector - gets first element with class 'test' querySelector-获取类为“ test”的第一个元素

var elementsList = document.querySelectorAll(".test");
for (var i = 0, len = elementsList.length; i < len; i++) {
  elementsList[i].style.minHeight = "30px";
}

query selectors are available in all modern browsers, from IE8. 查询选择器可用于IE8的所有现代浏览器中。 But in IE8 you can't use CSS3 selectors 但是在IE8中,您不能使用CSS3选择器

I assume this is what you are trying to do: 我想这就是你要做的:

fixTextPosition: function(){
 var width = $( window ).width();
 if (width > 1451){
  width = 1451;
 }
 var height = $( window ).height();
 if (height > 816){
  height = 816;
 }

 if (height < 816){
  $("#banner-resize").css("min-height", height - 1);
 }
 else{
  $("#banner-resize").css("min-height", height);
 }
}

If the above doesn't work, try this: 如果上述方法不起作用,请尝试以下操作:

 fixTextPosition: function(){
 var width = $( window ).width();
 if (width > 1451){
  width = 1451;
 }
 var height = $( window ).height();
 if (height > 816){
  height = 816;
 }

 if (height < 816){
  var ht = height - 1;
  $("#banner-resize").css("min-height", ht+" !important");
 }
 else{
  $("#banner-resize").css("min-height", height+" !important");
 }
}

Although I don't approve the above solution. 尽管我不同意上述解决方案。

My opinion would be you use CSS, 我的意见是您使用CSS,

 $bannerResize = $('$banner-resize')
 if (height < 816){
  $bannerResize.addClass('height815');
  $bannerResize.removeClass('height816')
 }
 else{
  $bannerResize.addClass('height816');
  $bannerResize.removeClass('height815')
 }
}

and CSS for the above should be, 和上面的CSS应该是

#banner-resize.height816{
  min-height:816px !important;
}
#banner-resize.height815{
  min-height:815px !important;
}

Hope that helps. 希望能有所帮助。

This is old but there is a right answer to this question so let's put it here for the record.这是旧的,但这个问题有一个正确的答案,所以让我们把它放在这里以作记录。

document.getElementById('banner-resize').style.setProperty('min-height', height, 'important');

What you were missing was the priority argument of the setProperty method of CSSStyleDeclaration .您缺少的是CSSStyleDeclarationsetProperty方法的priority参数。

priority Optional priority可选

A DOMString allowing the "important" CSS priority to be set.允许设置“重要”CSS 优先级的DOMString If not specified, treated as the empty string.如果未指定,则视为空字符串。 The following values are accepted:接受以下值:

  • String value "important"字符串值"important"
  • Keyword undefined关键字undefined
  • String empty value ""字符串空值""

https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty

However - as others have already pointed out and I'll just mention for the record -, unless something went terribly wrong, you should not need !important in element styles as these already have the highest specificity.但是 - 正如其他人已经指出的那样,我只是为了记录而提及 - 除非出现严重错误,否则您不需要!important元素样式,因为它们已经具有最高的特异性。

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

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