使用 jQuery 删除内联样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8790795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:02:31  来源:igfitidea点击:

Use jQuery to remove an inline style

jquerycss

提问by Scott Greenwald

<div class="mydiv">
  <ul style="height:111px; width:222px;">
    ...

Is it possible with jQuery (or plain javascript) to remove just the height attribute? Note that there are several instances of .mydiv ul with varying heights.

是否可以使用 jQuery(或普通 javascript)仅删除高度属性?请注意,有多个 .mydiv ul 实例具有不同的高度。

回答by Rory McCrossan

To remove an attribute, use this:

要删除属性,请使用以下命令:

$(".mydiv UL").css("height", "");

Thanks to am not i amfor correcting my answer.

感谢不是我纠正我的答案。

回答by DA.

Yes! You can!

是的!你可以!

If you set the css property to a blank value, it clears it.

如果您将 css 属性设置为空白值,它将清除它。

So, if your inline style is "background: pink" you can remove it via:

因此,如果您的内联样式是“背景:粉红色”,您可以通过以下方式将其删除:

$element.css('background','')

I'm pretty sure I learned that somewhere here on SE but, alas, don't have the specific source to site, so apologies for not being able to give credit where credit is due.

我很确定我在 SE 的某个地方了解到了这一点,但是,唉,没有特定的站点来源,因此对于无法在应得信用的情况下给予信用深表歉意。

UPDATE:

更新:

Well, I suppose I should have gone to the source. From the jQuery documentation (emphasis mine):

好吧,我想我应该去找源头。从 jQuery 文档(强调我的):

Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element.

将样式属性的值设置为空字符串 - 例如 $('#mydiv').css('color', '') - 如果该属性已被直接应用,则从元素中删除该属性,无论是在 HTML 样式中attribute,通过 jQuery 的 .css() 方法,或通过样式属性的直接 DOM 操作。但是,它不会删除已在样式表或元素中应用 CSS 规则的样式。

http://api.jquery.com/css/

http://api.jquery.com/css/

回答by schtsch

try this:

尝试这个:

$('.mydiv ul').height('');

回答by Silvertiger

Yes it is .. dpeneding on how you'd like to do it

是的 .. 取决于你想怎么做

<script type="text/javascript">
    function changestyle(element) {
        var currentstyle = document.getElementById(element).style.display;
        if (currentstyle == "inline") {
            document.getElementById(element).style.display = "";
            } else {
            document.getElementById(element).style.display = "inline";
        }
    }
</script>

this may vary slightly for different broswers but it should show you how to change it if you want.

对于不同的浏览器,这可能略有不同,但它应该会告诉您如何根据需要进行更改。

eek .. disregard .. jquery - much easier :)

eek .. 无视 .. jquery - 容易得多:)

回答by Kokers

add your style attribute to CSS and then you can add or remove class with these jQuery functions: addClassand removeClass

将您的样式属性添加到 CSS,然后您可以使用以下 jQuery 函数添加或删除类: addClassremoveClass

回答by raymondralibi

$('.mydiv ul').each(function(){
    $(this).attr('style', $(this).attr('style').replace('height: ', 'height:').replace('height:'+$(this).css('height')+';', '')); 
});

just make sure the style value format either "height:111px;"or "height: 111px;"

只需确保样式值格式"height:111px;""height: 111px;"