简体   繁体   English

何时以及如何在具有跨域的小书签中使用!important CSS属性?

[英]When and how to use !important CSS property in a bookmarklet with cross domains?

I have a bookmarklet and recently i have changed the CSS properties, using cleanslate css because the bookmarklet design was not the same on third party websites, when used. 我有一个bookmarklet ,最近我使用cleanslate css更改了CSS属性,因为在使用第三方书签时,第三方网站上的书签设计并不相同。

This css file, should reset all the properties of all the tags within a specific tag, and it does that, but when i need to manipulate the settings, i cant; 这个css文件,应该重置特定标签内所有标签的所有属性,并且这样做,但是当我需要操纵设置时,我不能;

<div class="mainclass">
<div id="login">a</div>
<div id="form">b</div>
<div id="details">c</div>
</div>

I have many forms, wich i hide and unhide at specific actions, like: i show the login form, if the user in not logged in, i show the the detail form, if the user is logged in and if i know the history of a url, and other forms; 我有很多表单,可以隐藏和取消隐藏特定的动作,例如:我显示登录表单,如果用户未登录,则显示详细信息表单,如果用户已登录并且我知道历史记录网址和其他形式;

So, if i set the div id details display property to block , and the rest to none , and then try to set all to none and the div id form display property to block , the css it doesnt do anything, but i can see that the javascript runs the actions 因此,如果我将div id details display属性设置为block ,其余设置为none ,然后尝试将所有属性none设置为none并且将div id form display属性设置为block ,则css不会执行任何操作,但是我可以看到javascript运行动作

This css should do the job, but because i used the !important property on all the tags properties, i cant set from javascript new values to properties, like: 这个CSS应该可以完成这项工作,但是由于我在所有标签属性上都使用了!important属性,因此无法从javascript新值设置为属性,例如:

document.getElementById("form").style.display = "none";

在此处输入图片说明

in this image, i use the old css, a reset method was used, and also, the js code worked fine, but the design was not the same on third party websites 在此图像中,我使用了旧的CSS,使用了重置方法,并且js代码工作正常,但是在第三方网站上的设计并不相同

in the actual project, the design is the same on all the third party websites, but the js code cant set css propertyes 在实际项目中,所有第三方网站上的设计都相同,但是js代码无法设置css属性

Important is a really bad idea in general and should try to be avoided as it breaks the rules of specificity in css. 一般而言,重要的想法实际上是一个坏主意,应避免使用它,因为它破坏了CSS的特异性规则。 Is there no way you can make your css more specific to overide the current styles rather than using !importnat. 没有办法让您的CSS更具体,以覆盖当前样式,而不是使用!importnat。

You can still apply importnant by doing some thing like this 您仍然可以通过执行类似这样的操作来应用重要元素

element.style.setProperty("display", "none", "important");

Or jquery makes it quite easy 或jquery使它变得相当容易

element.css("display","none !important");

But again best to just not use important 但是最好不要使用重要的

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

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