javascript - 样式显示 none 未在函数中触发
问题描述
我有一个功能:
$(".left_filter_menu").on("click", ".platforms", function() {
document.getElementById("loaderContainer").style.display = "block";
console.log(document.getElementById("loaderContainer"));
areAllPlatformSwitchesChecked();
document.getElementById("loaderContainer").style.display = "none";
console.log(document.getElementById("loaderContainer"));
});
这应该显示我的 loader div
,执行功能areAllPlatformSwitchesChecked
然后隐藏 loader div
,但它根本不显示 loader 。我添加了console.log
看看为什么会这样,输出是:
(index):731 <div id="loaderContainer" style="display: none;">…</div>
(index):734 <div id="loaderContainer" style="display: none;">…</div>
当我摆脱hide
零件时,它会显示 loader div
。有任何想法吗?
解决方案
我不知道这实际上是你的问题,但它通常是值得尝试的。
在我看来,元素将设置为可见,执行同步方法,然后将元素设置回隐藏,所有这些都在 UI 有机会更新之前完成。您可以简单地通过给浏览器一个机会来强制更新 UI,并使用setTimeout()
阻塞代码,就像这样......
$(".left_filter_menu").on("click", ".platforms", function() {
document.getElementById("loaderContainer").style.display = "block";
setTimeout(function() {
areAllPlatformSwitchesChecked();
document.getElementById("loaderContainer").style.display = "none";
}, 1);
}
试试看。老实说,这是在黑暗中开枪,但在你的情况下,我已经多次面对它,值得一试。让我知道它是否有效。如果没有我会删除这个。
推荐阅读
- c# - 删除早于修复日期的项目并替换第一次出现
- c - 查找最大数 (C) - 代码不起作用
- python-3.x - 如何在 Jupyter 笔记本输出单元中禁用换行?
- botframework - 在 Microsoft BotFramework WebChat 中模拟业务开发环境的 Skype
- javascript - Joi 验证:单个字段的多个验证错误消息
- office-ui-fabric - 将项目拖放到空的详细信息列表中
- excel - vba将具有给定名称的excel文件保存到特定位置
- postgresql - postgresql 中的 uuid_generate 性能缓慢
- javascript - 我可以在 amchart 图中保存状态吗?
- javascript - 如何在悬停时显示 D3 节点的文本?