Posts tagged "app":
15 Aug 2024
Web learning in practice
This post records the basic web development knowledge I have learned in practice.
1. Basic html structure
<!DOCTYPE html> <html lang="en"> <!-- A head --> <head> <meta charset="UTF-8"> <!-- to accommodate different screen size --> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- A tab name --> <title>A website</title> <link rel="stylesheet" href="styles.css"> </head> <!-- A body --> <<body> <!-- A page header --> <header> <h1>Welcome</h1> </header> <!-- A navigation panel --> <nav> <ul> <<li><a href="index.html">Home</a></li> </ul> </nav> <!-- The main content --> <main> <section id="home"> <h2>Home</h2> <p>Welcome!</p> </section> </main> <!-- A footer --> <footer> <p>© All rights reserved.</p> </footer> </body> </html>
2. Tags
<a>
: contain links; have following attributes:target="_blank"
: open the link in a new tab.title="Go to the link"
: the tooltip message, i.e., the floating message when a user hovers over.
<span>
: don’t add line breaks before or after it.<hr>
: horizontal rule.
3. Attributes
id="home"
:- allow specific styling of the element, e.g.,
#home ul {...}
only styles theul
in the block with the same id.
- allow specific styling of the element, e.g.,
4. Javascript
4.1. Fetch from an HTML URL
await fetch(url)
: returns aResponse
object.await
waits for the method to complete.
await response.text()
: returns thehtml
string.DOMParser().parseFromString(postHtml, "text/html")
: returns aDocument
object, which is a complete DOM tree from the HTML string;DOMParser
is a built-in browser API.postDoc.getElementById("content")
: returns anElement
with thecontent
id name.content.querySelector(".post-title a")
: fetches the firstElement
with thepost-title
class name, and returns the firstElement
the first<a>
(anchor) tag..
for classes,#
for IDs (IDs should be unique within a page)
4.2. Modify a DOM
content.querySelector(".taglist").remove()
: removes the element from the DOM, i.e., it modifiescontent
.
4.3. Syntax
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
: add a backslash to any special character in the query;$&
is a special pattern used in Javascript’sreplacement
method.