简体   繁体   English

如何撤销 element.style 属性的设置?

[英]How can I undo the setting of element.style properties?

I have an element in my document that has a background color and image set through a regular CSS rule.我的文档中有一个元素,它具有通过常规 CSS 规则设置的背景颜色和图像。

When a certain event happens, I want to animate that item, highlighting it (I'm using Scriptaculous, but this question applies to any framework that'll do the same).当某个事件发生时,我想为该项目制作动画,突出显示它(我使用的是 Scriptaculous,但这个问题适用于任何将执行相同操作的框架)。

new Effect.Highlight(elHighlight, { startcolor: '#ffff99', endcolor: '#ffffff', afterFinish: fnEndOfFadeOut });

The problem i'm facing is that after the animation is done, the element is left with the following style (according to FireBug):我面临的问题是,在 animation 完成后,该元素将保留以下样式(根据 FireBug):

element.style {
  background-color:transparent;
  background-image:none;
}

Which overrides the CSS rule, since it's set at the element level, so I'm losing the background that the item used to have...它覆盖了 CSS 规则,因为它是在元素级别设置的,所以我失去了该项目曾经拥有的背景......

What I'm trying to do is, in the callback function I'm running after the animation is done, set the style properties to a value that'll make them "go away".我想做的是,在 animation 完成后运行的回调 function 中,将样式属性设置为一个值,使它们“消失”。

var fnEndOfFadeOut = function() {
  elHighlight.style.backgroundColor = "xxxxx";
  elHighlight.style.backgroundImage = "xxxxx";
}

What I'm trying to figure out is what to put in "xxxx" (or how to do the same thing in a different way).我想弄清楚的是在“xxxx”中放入什么(或者如何以不同的方式做同样的事情)。
I tried 'auto', 'inherit', and '' (blank string), and neither worked (I didn't really expect them to work, but I'm clueless here).我尝试了“自动”、“继承”和“”(空白字符串),但都没有用(我真的没想到它们能用,但我在这里一无所知)。

I also tried elHighlight.style = "";我也试过elHighlight.style = ""; which, expectably, threw an exception.不出所料,这引发了异常。

What can I do to overcome this?我该怎么做才能克服这个问题?
I know I can put a span inside the element that I'm highlighting and highlight that span instead, but I'm hoping I'll be able to avoid the extra useless markup.我知道我可以在要突出显示的元素内放置一个跨度并改为突出显示该跨度,但我希望我能够避免额外的无用标记。

Chances are you're not setting the style on the correct element.您可能没有在正确的元素上设置样式。 It's probably being set somewhere up the line in a parent node.它可能被设置在父节点中的某处。

elHighlight.style.backgroundColor = "";
elHighlight.style.backgroundImage = "";

You can also remove all the default styling by calling:您还可以通过调用以下命令删除所有默认样式:

elHighlight.style.cssText = "";

In any case, you'll still have to do this on the specific element that is setting these properties, which means you may need to do a recursion on parentNode until you find it.在任何情况下,您仍然必须对设置这些属性的特定元素执行此操作,这意味着您可能需要对 parentNode 进行递归,直到找到它。

Try尝试
elHighlight.style.removeProperty('background-color') elHighlight.style.removeProperty('background-image') elHighlight.style.removeProperty('background-color') elHighlight.style.removeProperty('background-image')

have you tried elHightlight.style.background = "";?你试过 elHightlight.style.background = ""; 了吗?

I have a highlighter code on my site and this works我的网站上有一个荧光笔代码,这个有效

function highlight(id) {
var elements = getElementsByClass("softwareItem");
for (var ix in elements){
    elements[ix].style.background = ""; //This clears any previous highlight
}
document.getElementById(id).style.background = "#E7F3FA";
}

An HTML element can have multiple CSS classes.一个 HTML 元素可以有多个 CSS 类。 Put your highlight information inside a CSS class. Add this class to your element to highlight it.将您的突出显示信息放在 CSS class 中。将此 class 添加到您的元素中以突出显示它。 Remove the class to undo the effect.删除 class 以撤消效果。

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

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