简体   繁体   English

通过javascript将CSS属性设置为重要

[英]Set a css attribute to important through javascript

On my page I want to set the attribute display to block!important when there's a specific string in the url, 在我的页面上,我想将属性显示设置为block!important当url中有特定的字符串时,

I've been doing some research, but can't find the correct answer. 我一直在做一些研究,但是找不到正确的答案。 Using the code below I can set the background-color: green!important on the body, but I don't know how to target something else (the "loginActive" id element). 使用下面的代码,我可以在主体上设置background-color: green!important ,但是我不知道如何定位其他对象( "loginActive" id元素)。

:javascript
  if(window.location.href.indexOf("sign_up") > -1) {
    document.body.style.setProperty ("background-color", "green", "important");
    document.getElementById("loginActive").style.display = "block", "important";
  }

Any tips on how I can set display: block!important on the element #loginActive ? 关于如何设置display: block!important任何提示display: block!important #loginActive元素上的display: block!important的吗?

Try this 尝试这个

var item = document.getElementById("loginActive");
item.style. display = 'block !important';

Or 要么

item.setAttribute('style', 'display:block !important');

But you shouldn't do that 但是你不应该那样做

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

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