我需要根据从 djangoviews.py 传入的参数设置一个 div 来显示或隐藏,但我就是无法让它工作。我的代码如下:
在正文中:
{% if report_type == "detail" %}
visibility("detail");
{% endif %}
在 JavaScript 中:
<script type="text/javascript">
function visibility(reportType) {
console.log(reportType);
if(reportType == "detail") {
$('#executive_summary').style.display = 'none';
}
}
</script>
但是上面的代码不起作用。有人可以给我一些建议吗?谢谢。
最佳答案
通过纯CSS:
{% if report_type != "detail" %}
<div style="display: none;">
{% else %}
<div style="display: block;">
{% endif %}
...
</div>
或者使用 JavaScript (jQuery):
{% if report_type != "detail" %}
<script type="text/javascript">
$('#executive_summary').hide();
</script>
{% endif %}
通过 JavaScript 函数:
<script type="text/javascript">
/* If wanted to run when page is loaded. */
$(function() {
// Handler for .ready() called.
visibility("detail");
});
function visibility(reportType) {
if(reportType == "detail") {
$('#executive_summary').show();
}
else {
$('#executive_summary').hide();
}
}
</script>
关于javascript - python Django : How can I determine if a div should show or hide based on the parameter passed to template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10212326/