Django 模板中的 Javascript
Posted
技术标签:
【中文标题】Django 模板中的 Javascript【英文标题】:Javascript inside Django Template 【发布时间】:2020-12-12 00:06:36 【问题描述】:我在 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”代表缺陷。
【问题讨论】:
什么意思?你能详细说明你想做什么吗? 它没有给出任何结果。数组pageWarn[1]
的最后一个元素是div
标签的id,我想将消息插入到索引为0 的div
标签内,即pageWarn[0]
。
能否请您发布您的完整html?
请看一下。我已经发布了。
您的 html 和 javascript 看起来不错。 pagestruc
返回的是什么?您是否尝试将 pagestruc
替换为任何其他整数值?
【参考方案1】:
我认为 django 标签之外的用户 qoutes 是一个更好的主意。var pageStrucVal = parseInt(' pagestruc ');
【讨论】:
【参考方案2】:试试var pageStrucVal = parseInt( pagestruc )
;
【讨论】:
它按照你的想法工作。var pageStrucVal = parseInt( pagestruc );
是正确答案。
这意味着检查绿色对勾按钮。对不起,我是菜鸟。如果“是”,我已经完成了。
你的另一个好处。你能帮我减少代码的长度吗?
对不起,我看不出哪里可以简化。您可以使用 javaScript minifier 或其他一些工具来缩小它。以上是关于Django 模板中的 Javascript的主要内容,如果未能解决你的问题,请参考以下文章