如何使用 .style JavaScript 属性添加 css box-shadow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32627878/
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-10-28 15:32:02  来源:igfitidea点击:

How to add css box-shadow using .style JavaScript property

javascripthtmlcss

提问by Escher

I've got the following JavaScript snippet:

我有以下 JavaScript 片段:

document.getElementById("imgA").style.box-shadow = "0 0 5px #999999";

The hyphen in box-shadowis causing the JavaScript engine to throw an invalid assignment exception (in Firefox). Doing "box-shadow"or 'box-shadow'doesn't work. Is there a good way around this without using jquery's .css()method?

连字符 inbox-shadow导致 JavaScript 引擎抛出无效赋值异常(在 Firefox 中)。做"box-shadow"'box-shadow'不工作。有没有不使用 jquery.css()方法的好方法?

回答by Joel Almeida

You can use style["boxShadow"]or style.boxShadow.

您可以使用style["boxShadow"]style.boxShadow

document.getElementById("foo").style["boxShadow"] = "0 0 5px #999999";
<div id="foo">12123123</div>

回答by Tushar

CSS properties with a -are represented in camelCasein Javascriptobjects. So you dont need a hyphen -just write boxShadow

带有 的 CSS 属性在对象-中表示。所以你不需要连字符就写camelCaseJavascript-boxShadow

document.getElementById("shadow").style["boxShadow"] = "0 0 5px #999999";
<div id="shadow">Tushar </div>

回答by Gabriele Petrioli

Use boxShadow

利用 boxShadow

document.getElementById("imgA").style.boxShadow = "0 0 5px #999999";

回答by Lee

document.getElementById('redbox').style.boxShadow = "0 0 3px #000";

document.getElementById('redbox').style.boxShadow = "0 0 3px #000";

回答by Steevan

This should work.

这应该有效。

document.getElementById("demo").style.boxShadow = "0 0 5px #999999";