首页 > 解决方案 > Django 模板中的 Javascript

问题描述

我在 Django 模板中使用 JS,但 JS 代码没有响应。该怎么办?

这个内部<script>标签:

 var pageStrucVal = {{ pagestruc }};
        if (pageStrucVal >= 82){
            var pageWarn = ["Brilliant, your site's structure and design is awesome and rocking. <gold>RockingStructure</gold>", 'pp']
        }
            
        else if ( pageStrucVal >= 72){
            var pageWarn = ["Nice, your site's structure and design is suitable", 'pp']
            }
            
        else if ( pageStrucVal < 45){
            pageWarn = ["Extremely bad condition, your site's structure and design is extremely bad and unmanaged. Strictly to use a different theme or change the interface (UI). <red>#ExtremelyBad</red>", 'dd'];
            }
        else if ( pageStrucVal < 50){
            pageWarn = ["Worst conditon, the content quality of your site is too much poor. Use a well-managed theme or change the interface (UI).<orange>#ReallyPoor</orange>", 'dd'];
            }
        
        else if ( pageStrucVal < 55){
            pageWarn = ["Too much bad, your site's structure and design is too much bad and unmanaged. Try to use a different theme or change the interface (UI).<orange>#VeryPoor", 'dd'];
            }
        
        else if ( pageStrucVal < 63){
            pageWarn = ["Very bad, your site's structure and design is very unmanaged. You can try to use a different theme or change the interface (UI).<orange>#MuchBad</orange>", 'dd'];
            }
        
        else if ( pageStrucVal < 72){
            pageWarn = ["Bad, your site's structure and design isn't suitable. Small change in the interface (UI) is required.<green> #TendsToBad</green>", 'dd'];
            }
        console.log(pageWarn);
        document.getElementById(pageWarn[1]).appendChild(document.createElement('p')).innerHTML = (pageWarn[0]);
    

书写是否document.getElementById(pageWarn[1]).appendChild(document.createElement('p')).innerHTML = (pageWarn[0]);正确且位置正确?

这是HTML:

<div class='plusNdefects' id='moredetails'>
            <h1>Good Aspects and Plus Points.</h1>
            <p class='details'>Here are some awesome good aspects and benefits you have in your site to get Google Adsense.</p>
        
            <!-- Ads ::> -->
            {% include "ads/outads.html" %}
            <!-- Ads <:: -->
        
            <div class='persona'>
                <div id='pp'> </div>
            </div>
        </div>
        
        <div class='plusNdefects'>
            <h1>Disabilities and Improvements.</h1>
            <p class='details'>These are some disabilities of your website that are stopping you from being approved by Google
                Adsense.</p>
        
            <!-- Ads ::> -->
            {% include "ads/outads.html" %}
            <!-- Ads <:: -->
        
            <div class='persona'>
                <div id='dd'></div>
            </div>

'pp' 和 'dd' 是我要插入消息的 id(位置)。“pp”代表加分,“dd”代表缺陷。

标签: javascriptarraysdjangodjango-templatesjavascript-objects

解决方案


我认为 django 标签之外的用户 qoutes 是一个更好的主意。var pageStrucVal = parseInt('{{ pagestruc }}');


推荐阅读