【发布时间】:2019-04-11 10:54:48
【问题描述】:
我计算列出的项目并根据项目的数量添加内联样式。但是,内联样式显示为空。
var liList = document.getElementById("reports-list").getElementsByTagName("li");
var listCount = liList.length;
if (listCount == 0) {
$('#download-reports').css({'display': 'none !important'});
$('#welcome-left').css({'width': '100% !important'});
} else if (listCount >= 1) {
$('#download-reports').css({'display': 'block !important'});
$('#welcome-left').css({'width': '65% !important'});
}
【问题讨论】:
-
我们需要更多上下文来了解何时运行此逻辑,尤其是在页面上创建元素的时间。
-
您可以在您的逻辑中
console.log($('#download-reports').length)并在逻辑运行时验证它是否正在查找元素。 -
实际上您的问题很可能是您尝试使用
!importantinline。据我所知,jQuery 不会让你这样做。你需要删除那些 -
你使用
.css()而不是.hide()和.show()有什么原因吗? -
else if (listCount >= 1)没有意义。只需使用else,因为这是唯一的另一种可能性。
标签: javascript jquery html css styles