The Document Object Model built on the browser has an important role in building dynamic web applications.
基于浏览器构建的文档对象模型在构建动态Web应用程序中具有重要作用。
Today we'll look at how we can inject content and change content on the web page using JavaScript DOM by building a very simple web application.
今天,我们将研究如何通过构建一个非常简单的Web应用程序来使用JavaScript DOM在网页上注入内容和更改内容。
The HTML DOM can be accessed using JavaScript. This is usually called The DOM Programming Interface where the programming interface is the properties and methods of each object. The property represents a value that we can get or set by changing the HTML elements on the page method is some action we can perform like adding or deleting some HTML. We'll look at both by building a simple application.
可以使用JavaScript访问HTML DOM 。 通常将其称为DOM编程接口,其中编程接口是每个对象的属性和方法。 该属性表示我们可以通过更改page方法上HTML元素获得或设置的值,这是我们可以执行的一些操作,例如添加或删除一些HTML。 我们将通过构建一个简单的应用程序来研究两者。
We'll build a simple To-Do application where we can add to-do items on the web page and mark them done by clicking them. Navigate to the directory of your choice and create an index.html file and add the following starter template,
我们将构建一个简单的待办事项应用程序,在其中可以在网页上添加待办事项并通过单击将它们标记为完成。 导航到您选择的目录并创建index.html文件,然后添加以下入门模板,
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>DOM JS</title>
</head>
<body>
<h1>To Do List</h1>
<h3>A simple to do list using JavaScript DOM</h3>
<p>Add items to the list using the add button and mark them done by clicking them</p>
<div class="container">
<br>
<input type="text" placeholder="Type an item">
<button id="btn">Add Item!</button>
<ul class="list">
</ul>
</div>
<script>
</script>
</body>
</html>
Add some styles in the styles.css file,
在styles.css文件中添加一些样式,
body {
background: whitesmoke;
}
h1 {
margin: 5px auto;
padding: 20px;
color: brown;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
text-align: center;
}
p {
text-align: center;
color: black;
font-family: Arial, Helvetica, sans-serif;
}
.container {
max-width: 400px;
padding-left: 400px;
text-align: center;
}
h3 {
text-align: center;
color: blueviolet;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
input {
text-decoration: none;
height: 40px;
background: rgba(186, 217, 230, 0.521);
border: 1px solid black;
text-align: center;
letter-spacing: 2px;
font-family: monospace;
}
button {
text-align: center;
border: 2px solid black;
border-radius: 5px;
padding: 10px;
background: steelblue;
color: white;
text-decoration: none;
cursor: pointer;
}
ul {
list-style-type: none;
}
.item {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-size: 20px;
cursor: pointer;
}
.done {
text-decoration: line-through tomato;
}
HTML Output:
HTML输出:
Alright now that we have some basic styles setup we'll break down our logic. We need a reference to our empty list, the <ul> tag in our HTML where we'll insert items. We also need a reference to the button that will allow us to add items to the list and one to the input field. So let's do this first,
好了,现在我们有了一些基本的样式设置,我们将分解逻辑。 我们需要引用我们的空列表,即HTML中的<ul>标记,我们将在其中插入项目。 我们还需要对按钮的引用,该按钮使我们可以将项目添加到列表中,并将一个添加到输入字段中。 所以让我们先做
<script>
var addItem=document.querySelector('#btn');
var list=document.querySelector('.list');
var input=document.querySelector('input');
</script>
Now we need to attach a click type event listener to our button. You can read more about event listeners here. Inside the event listener, we'll get a reference to the text that the user has typed in the input field and then append that text on the list. We'll create some HTML using a template string and add that to our list using the innerHTML property.
现在,我们需要将点击类型事件监听器附加到我们的按钮上。 您可以在此处阅读有关事件侦听器的更多信息。 在事件侦听器内部,我们将获得对用户在输入字段中键入的文本的引用,然后将该文本追加到列表中。 我们将使用模板字符串创建一些HTML,并使用innerHTML属性将其添加到列表中。
<script>
var input=document.querySelector('input');
var newItem=input.value;
var addItem=document.querySelector('#btn');
var list=document.querySelector('.list');
addItem.addEventListener('click',e=>{
var newItem=document.querySelector('input').value;
var html='<li><span class="item">${newItem}</span></li>';
list.innerHTML+=html;
})
</script>
For a better user interface, we must clear the input field since we have already appended that item on the page. We can do this by setting the input field's value to an empty string,
为了获得更好的用户界面,我们必须清除输入字段,因为我们已经在页面上添加了该项目。 为此,我们可以将输入字段的值设置为空字符串,
input.value = "";
HTML Output:
HTML输出:
Great! Now we can append items to the list and we have done all this using JavaScript by manipulating the HTML DOM. Now the only thing left is to mark the items on the list as done by clicking them. Head over to the stylesheet and see that we have a class done which styles our span tags or the list items differently.
大! 现在我们可以将项目添加到列表中,并且通过使用HTML DOM来使用JavaScript完成所有这些操作。 现在剩下的唯一事情就是通过单击将它们标记为完成。 转到样式表,看看我们完成了一个类,以不同的方式为span标签或列表项设置样式。
All we have to do is attach an event listener to our items and add this done class to these items. So let's do this,
我们要做的就是将事件侦听器附加到我们的项目并将此完成的类添加到这些项目。 因此,让我们这样做
list.addEventListener('click',e=>{
//console.log(e.target.innerHTML);
e.target.classList.toggle('done');
})
HTML Output:
HTML输出:
We're done now! Have a look at all the JavaScript that we have written to manipulate DOM,
现在完成! 看看我们编写的用于处理DOM的所有JavaScript,
<script>
var input=document.querySelector('input');
var addItem=document.querySelector('#btn');
var list=document.querySelector('.list');
addItem.addEventListener('click',e=>{
var newItem=input.value;
//console.log(newItem);
var html='<li><span class="item">${newItem}</span></li>';
list.innerHTML+=html;
input.value="";
})
list.addEventListener('click',e=>{
//console.log(e.target.innerHTML);
e.target.classList.toggle('done');
})
</script>
Voila! You can now mark the items done by clicking them and mark them undone again by clicking them. We have made this super simple application only by manipulating the DOM through JavaScript.
瞧! 现在,您可以通过单击将它们标记为完成,然后通过单击将它们标记为撤消。 仅通过通过JavaScript操纵DOM,我们才使此超级简单的应用程序成为可能。
As an exercise you can add another feature to this app. Add a button which removes all the items from the list. Think, it’ll be a cakewalk to implement this now that you have a hang of JavaScript DOM.
作为练习,您可以向此应用添加其他功能。 添加一个按钮,从列表中删除所有项目。 想想,既然您已经掌握了JavaScript DOM,那么实现此目标将是步履蹒跚。
翻译自: https://www.includehelp.com/code-snippets/the-javascript-dom.aspx
2528




被折叠的 条评论
为什么被折叠?



