有没有办法用一行 JavaScript 代码来改变两个或多个 Div 样式?
document.getElementById("searchScroll").style.position="fixed";
document.getElementById("searchScroll").style.margin="-50px";
是否可以将代码合并到一行中?
最佳答案
您可以使用单行 JavaScript 来更新它 - 但我建议将其保留为多行 - 使用多行更容易阅读和理解,并且您不必担心替换其他样式值。
您可以上一级并设置样式
,例如:
document.getElementById("searchScroll").style.cssText = "position:fixed;margin:-50px;";
这将替换当前样式..要添加您可以使用以下内容:
document.getElementById("searchScroll").style.cssText += "position:fixed;margin:-50px;";
关于javascript - 如何在 Javascript 中更改两个或多个 Div 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569258/