我们推荐的开发环境是 VSCode。这是适合开发的一款文本编辑器。机房的电脑一般来说都预装好了。这款编辑器因为强大的功能丰富的插件生态深受程序员欢迎。我们写 HTML 可以装一个 Live server 插件,这是一个简单的测试服务器,可以方便调试网页。直接用浏览器打开 HTML 文件是不被推荐的做法,因为如果涉及一些外部 CSS 样式和 JS 脚本,这样很可能会导致功能异常。所以通过一个测试服务器完成。
请给每个项目一个单独的文件夹,然后用 VSCode 打开这个文件。因为 VSCode 假设一个项目占据一个独立的文件夹,如果只是单独打开一个文件,会导致部分功能无法使用(包括我们的 Live Server)。
<!DOCTYPE html> <head> <title>ZHESHI YIGE BIAOTI</title> </head> <h1>WRITER:潘裕捆 grade10 class</h114> <P> <P>建议加强司空震!<hr>司空震在这个赛季!<hr>竟然还被策划<strong>削弱!</strong></P> <p>(打中文太累了我想打拼音)</p> <p>yi xia shi dui xue ruo SKZ de ti hui </p> <p>shou xian shi fan xia<hr><strong>傲慢</strong>之罪的加罗</p> <p>qi ci shi fanxia<hr><strong>嫉妒</strong>之罪的李信</p> <p>weiwandaixu...(qi shi shi xia ke le )</p> </P>
正确版本
点击此处展开
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!DOCTYPE html> <htmllang="zh-cn"> <head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <title>ZHESHI YIGE BIAOTI</title> </head> <body> <h1>WRITER:潘裕捆 grade10 class</h1> <p>建议加强司空震!<hr>司空震在这个赛季!<hr>竟然还被策划<strong>削弱!</strong></p> <p>(打中文太累了我想打拼音)</p> <p>yi xia shi dui xue ruo SKZ de ti hui </p> <p>shou xian shi fan xia<hr><strong>傲慢</strong>之罪的加罗</p> <p>qi ci shi fanxia<hr><strong>嫉妒</strong>之罪的李信</p> <p>weiwandaixu...(qi shi shi xia ke le )</p> </body> </html>
有了基本的需求,下一步要进行的就是程序的设计了。这个程序设计,我们按照管理分成两个部分。UI 和逻辑。UI,就是所谓的 User Interface,是直接展现给用户的部分,能够给用户看见,相当于一个外壳。逻辑呢,就是内部的操作,这些用户没法直接看见,但是给程序提供了内部的功能。两者合起来,就是一个完整的程序了。我们分别进行设计分析。
# supported colors: blue, green, purple, red, orange or slate. If you need clear, leave it empty. # cover_color: red
# The blog button should not be removed. blog_button: title: Blog description: Visit blog
# Navigation buttons in the front page. nav: - {title: Projects, description: My Projects, url: 'https://github.com/AmazingChen/VHabit'} # - {title: Another Button, description: A button, url: 'http://example.com'}
浏览器 API 内置于 Web 浏览器中,能从浏览器和电脑周边环境中提取数据,并用来做有用的复杂的事情 。我们的 DOM 就是浏览器 API。
第三方 API 缺省情况下不会内置于浏览器中,通常必须在 Web 中的某个地方获取代码和信息。例如大名鼎鼎的 JQuery,一个轻量级前端框架,适合一些简单的网页。
浏览器内部的 API 又分这么多种:
操作文档的 API 内置于浏览器中。最明显的例子是DOM(文档对象模型)API,它允许您操作HTML和CSS — 创建、移除以及修改HTML,动态地将新样式应用到您的页面,等等。每当您看到一个弹出窗口出现在一个页面上,或者显示一些新的内容时,这都是DOM的行为。 您可以在在Manipulating documents中找到关于这些类型的API的更多信息。
从服务器获取数据的API 用于更新网页的一小部分是相当好用的。这个看似很小的细节能对网站的性能和行为产生巨大的影响 — 如果您只是更新一个股票列表或者一些可用的新故事而不需要从服务器重新加载整个页面将使网站或应用程序感觉更加敏感和“活泼”。使这成为可能的API包括XMLHttpRequest和Fetch API。您也可能会遇到描述这种技术的术语Ajax。您可以在Fetching data from the server找到关于类似的API的更多信息。
<!DOCTYPE html> <html> <head> <metacharset="utf-8"> <title>Simple DOM example</title> </head> <body> <section> <imgsrc="dinosaur.png"alt="A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth."> <p>Here we will add a link to the <ahref="https://www.mozilla.org/">Mozilla homepage</a></p> </section> </body> </html>
显示成为一个树状结构是这样子的:
看到这个你想到了什么?对,就和电脑的文件夹一样,一层一层往下套。我们把每个 HTML 元素对应在树中的入口称为节点。我们有这么几个术语:
let text = document.createTextNode(' — the premier source for web development knowledge.');
现在获取内部连接的段落的引用,并把文本节点绑定到这个节点上:
let linkPara = document.querySelector('p');
linkPara.appendChild(text);
现在 HTML 当中的 section 部分变成了这样:
1 2 3 4 5
<section> <imgsrc="dinosaur.png"alt="A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth."> <p>Here we will add a link to the <ahref="https://developer.mozilla.org">Mozilla Developer Network</a></p> <p>We hope you enjoyed the ride.</p> — the premier source for web development knowledge. </section>
let link = document.querySelector('a'); link.textContent = 'Mozilla Developer Network'; link.href = 'https://developer.mozilla.org'; let sect = document.querySelector('section'); let para = document.createElement('p'); para.textContent = 'We hope you enjoyed the ride.'; sect.appendChild(para); let text = document.createTextNode(' — the premier source for web development knowledge.'); sect.appendChild(text);
就可以重新变成这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
let myTest = { run: function() { let link = document.querySelector('a'); link.textContent = 'Mozilla Developer Network'; link.href = 'https://developer.mozilla.org'; let sect = document.querySelector('section'); let para = document.createElement('p'); para.textContent = 'We hope you enjoyed the ride.'; sect.appendChild(para); let text = document.createTextNode(' — the premier source for web development knowledge.'); sect.appendChild(text); } }; myTest.run();
占用的名称从 link, sect, para, text 这四个一下变成了只有一个 myTest,可以有效减少撞车机率。