简体   繁体   English

如何从 HTML 标签中删除 CSS 属性

[英]how to remove CSS property from a HTML tag

I am unable to remove CSS property for a HTML tag.我无法删除 HTML 标记的 CSS 属性。

I am trying to remove position style property from css.我正在尝试从 css 中删除位置样式属性。 Actually it's coming from parent, I am able to override it with my css file but i want to remove postion property completely as per my requirement.实际上它来自父级,我可以用我的 css 文件覆盖它,但我想根据我的要求完全删除 postion 属性。

Below code, I tried, But it didn't work for me下面的代码,我试过了,但对我不起作用

$("p").removeProp("position");

Example for my code (Note: I don't have any className for P tag)我的代码示例(注意:我没有 P 标签的任何 className)

 $("p").removeProp("position");
 p { position: static; font-size: 18px; color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p> Hello Stack Overflow </p>

You can remove them by:您可以通过以下方式删除它们:

$("p").css("position",'');

As mentioned in the jquery documentation:正如jquery文档中提到的:

Setting the value of a style property to an empty string — eg $(selector).css('color', ' ') — removes that property from an element if it has already been directly applied.将样式属性的值设置为空字符串 - 例如$(selector).css('color', ' ') - 如果该属性已被直接应用,则会从元素中删除该属性。

To remove the whole in-line style of an element use:要删除元素的整个内联样式,请使用:

$(selector).removeAttr('style');

Your actual problem seems that you have added css property on p tag.您的实际问题似乎是您在p标签上添加了css属性。 You should implement either use of class or use !important to replace.您应该实现使用class或使用!important来替换。 The best way is to use class最好的方法是使用class

 .p-static { position: static; font-size: 18px; color: red; } .p-normal { font-size: 18px; color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p class="p-static"> Hello Stack Overflow, this is static paragrap. </p> <p class="p-normal"> Hello Stack Overflow, this is normal paragraph. </p>

在 jQuery 的帮助下,您可以轻松实现这一目标

$("p").css({ 'postion': '' });

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

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