python django:如何根据传递给模板的参数确定div是否显示或隐藏

Sha*_*ang 0 javascript django jquery

我需要根据从django views.py传入的参数设置div以显示或隐藏,但是我无法使其工作。我的代码如下:

在身体里:

{% if report_type == "detail" %}
    visibility("detail");
{% endif %}
Run Code Online (Sandbox Code Playgroud)

在javascript中:

<script type="text/javascript">
    function visibility(reportType) {
        console.log(reportType);
        if(reportType == "detail") {
            $('#executive_summary').style.display = 'none';
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

但是,上面的代码不起作用。有人可以给我一些建议吗?谢谢。

nik*_*ela 5

通过纯CSS:

        {% if report_type != "detail" %}
            <div style="display: none;">
        {% else %}
            <div style="display: block;">
        {% endif %}
        ...
        </div>
Run Code Online (Sandbox Code Playgroud)

或使用JavaScript(jQuery):

    {% if report_type != "detail" %}
    <script type="text/javascript">
        $('#executive_summary').hide();
    </script>
    {% endif %}
Run Code Online (Sandbox Code Playgroud)

通过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>
Run Code Online (Sandbox Code Playgroud)