简体   繁体   English

如何覆盖已经从外部HTML标记为!important的内联CSS

[英]How to override inline CSS that is already marked !important from external HTML

I am using HTML to input a cloud-based instagram feed on a website. 我正在使用HTML在网站上输入基于云的instagram feed。 I want to hide the bottom half of the feed that references the website of the publisher. 我想隐藏引用发布者网站的feed的下半部分。 The inline HTML that is outputting already has display: inline-block !important marked, so trying to hide it with display: none !important or visibility: hidden !important do not work. 正在输出的内联HTML已经显示:inline-block!important已标记,因此尝试通过显示隐藏它:none!important或可见性:hidden!important不起作用。 I am able to hide the entire block with CSS but not "parts" of it. 我可以用CSS隐藏整个块,但不能隐藏它的“部分”。 I don't seem to be able to edit the HTML since it is coming from the cloud. 我似乎无法编辑HTML,因为它来自云。 The only HTML that I actually use on the site in order to make it render does not include any of the inline CSS, as it is only two lines. 我在网站上实际使用的唯一HTML以使其呈现,不包括任何内联CSS,因为它只有两行。

Would someone mind explaining how this kind of thing works and why I am encountering issues overriding the inline CSS? 有人介意解释这种事情是如何工作的,为什么我遇到覆盖内联CSS的问题吗?

I have tried using: 我试过使用:

display: inline !important , display: none !important and visibility: hidden !important display: inline !importantdisplay: none !importantvisibility: hidden !important

None of these are receiving priority, as the element.style in the chrome developer console clearly shows the inline CSS already marked as !important that I do not have access to, nor can I edit. 这些都不是优先级,因为chrome开发者控制台中的element.style清楚地显示了已经标记为!important的内联CSS,我无权访问,也无法编辑。

I simply want to hide one selector within the HTML. 我只想在HTML中隐藏一个选择器。 It is marked as "a" 标记为“ a”

a {
}

I have tried using this selector within the actual element, but it does not receive priority either. 我尝试在实际元素中使用此选择器,但它也不获得优先级。 Nothing is working. 什么都没用。

Try to remove the inline style with jQuery 尝试使用jQuery删除内联样式

 $("#myDiv2").css("transform","");//used rotate to see the effect 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div id="myDiv1" style="width:100px !important; height:100px !important;transform:rotate(30deg) !important"> //Some Content </div> <div id="myDiv2" style="width:100px !important; height:100px !important; transform:rotate(30deg) !important"> //Some Content </div> 

Setting the value of a style property to an empty string removes that property from an element. 将样式属性的值设置为空字符串会从元素中删除该属性。

$("#myDiv2").css("transform","");

Typically the only way to do this is to be more specific in your selector, for example selecting a[href][style] is more specific than just a . 通常,唯一的方法是在选择器中更具体,例如,选择a[href][style]不仅比a更具体。 But since this is inline styles and already using !important you unfortunately might be out of luck! 但是由于这是内联样式,并且已经在使用!important ,所以不幸的是您可能不走运!

 a[href][style]{ color: green !important; } 
 <a href="#" style="color: red !important;">This link uses !important</a> <br/> <a href="#" style="color: red;">This link does not</a> 

If you wanted to just remove the links without importing several thousand lines of jQuery code, you can select and remove all <a> tags with just a few lines of plain JS. 如果您只想删除链接而不导入几千行jQuery代码,则可以只用几行纯JS选择并删除所有<a>标记。

 document.querySelectorAll('a').forEach(link => { link.remove(); }); 
 <p> here is some text <a href="#" style="color: red !important;">This link uses !important</a> <br/> <a href="#" style="color: red;">This link does not</a> and some more text </p> 

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

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