javascript - 如何在 Django 中实现 JavaScript?
问题描述
我正在创建一个基本的待办事项网络应用程序,我正在努力完成最后一部分——在 Django 中实现 JavaScript。
我在静态文件夹中创建了一个单独.js
的文件。
// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
myNodelist[i].appendChild(span);
}
// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
}
}, false);
// Create a new list item when clicking on the "Add" button
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'js/app.js' %}"
<meta charset="utf-8">
<meta name="description" content="To-Do Web Application">
<meta name="author" content="Wesley Ross">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<title>To-Do Web Application</title>
<style>
body {
font-family: sans-serif;
text-align: center;
}
* {
box-sizing: border-box;
}
ul {
margin: 0;
padding: 0;
}
ul li {
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
background: #eee;
font-size: 18px;
transition: 0.2s;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
ul li:nth-child(odd) {
background: #f9f9f9;
}
ul li:hover {
background: #ddd;
}
ul li.checked {
background: #888;
color: #fff;
text-decoration: line-through;
}
ul li.checked::before {
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
}
.close:hover {
background-color: #f44336;
}
.header {
color: white;
background-color: #ff0000;
padding: 30px 40px;
color: white;
text-align: center;
}
.header:after {
content: "";
display: table;
clear: both;
}
input {
margin: 0;
border: none;
border-radius: 0;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
.addBtn {
padding: 10px;
width: 25%;
background: #d9d9d9;
color: #555;
float: left;
text-align: center;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
border-radius: 0;
}
.addBtn:hover {
background-color: #bbb;
}
</style>
<body>
<h1>Welcome {{user|title}}!</h1>
<div id="myDIV" class="header">
<h2>My To Do List</h2>
<input type="text" id="myInput" placeholder="Enter Task...">
<span onclick="newElement()" class="addBtn">Add</span>
</div>
<ul id="myUL">
<li></li>
<li class="checked">Listen to Inspirational audio or music</li>
<li class="checked">Read Bible scripture</li>
<li>Coding: blogs or videos</li>
<li>Coding: CodeWars</li>
<li>Pray</li>
<li>Read book: Unlocking the Bible</li>
</ul>
</body>
</html>
我是 Django 的新手,所以我可能缺少一些东西。
解决方案
JavaScript 代码放在<script>
标签内,所以不要link
写:
<script src="{% static 'js/app.js' %}"><script>
并且为了良好的实践{% load static %}
,在模板的开头编写,如果你不关心运行你的js 代码,当页面开始加载时,只需将你的脚本放在标签的末尾body
- 这对用户体验更好。
推荐阅读
- c# - 另一个类中的 JustMock 助手在测试中返回 null
- angular - Angular - 允许在 textarea 上输入键,但不允许在 formGroup 中
- kubernetes - 为什么安全 GRPC 调用无法到达入口网关?
- javascript - 如何访问 point.r 的当前值?
- bash - 使用一个for循环遍历多个目录
- java - 存储具有 KV 列的表的最佳数据结构是什么
- dom - Vaadin Flow:如何判断组件是否已附加
- azure-active-directory - AAD Connect 同步限制
- android - 使用 Vertical RecyclerView 拖动滚动条(快速滚动)时始终位于顶部偏移的项目
- node.js - 如何根据用户请求快速启动/取消功能