Notes

基本是直接拷贝代码了。。原书《Eloquent JavaScript》

1、fetch

Fetch vs Ajax - Xul.fr

Why I won't be using Fetch API in my apps – Shahar Talmi – Medium(这篇文章的评论区有精彩辩论:p)

Fetch API - Web APIs | MDN

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<script type="text/javascript">
let resp;
fetch("example/data.txt").then(response => {
console.log(response.status);
// → 200
console.log(response.headers.get("Content-Type")); // header是一种类似map的对象,大小写无关紧要
// → text/plain
resp = response;
}); // 只有在网络错误或者无法找到请求服务器时,promise才会被reject fetch("example/data.txt")
.then(resp => resp.text()) // 读取响应也是个耗时操作,因此仍然用promise来读它
.then(text => console.log(text));
// → hello world fetch("example/data.txt", {headers: {Range: "bytes=8-19"}})
.then(resp => resp.text())
.then(console.log);
// → the content(本机测试还是整段话输出来了?) // 默认是GET方法
fetch("example/data.txt", {method: "DELETE" }).then(resp => {
console.log(resp.status);
// → 405 (本机测试是500)
}).catch(reason => console.log(reason)); </script>
</body> </html>

2、form

form是在还没有js的时代被设计出来的,在有js的时代,form并不是必要的。

3、focus

可以用focus()和blur()方法控制focus:

<input type="text">
<script>
document.querySelector("input").focus();
console.log(document.activeElement.tagName);
// → INPUT
document.querySelector("input").blur();
console.log(document.activeElement.tagName);
// → BODY
</script>

自定义tab切换focus顺序:

<input type="text" tabindex=1> <a href=".">(help)</a>
<button onclick="console.log('ok')" tabindex=2>OK</button>

4、Disabled fields

例如在执行异步操作时,不希望用户反复点击按钮:

<button>I'm all right</button>
<button disabled>I'm out</button>

5、form’s elements property

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<form action="example/submit.html">
Name: <input type="text" name="name"><br> Password: <input type="password" name="password"><br>
<button type="submit">Log in</button>
</form>
<script>
let form = document.querySelector("form");
console.log(form.elements[1].type);
// → password
console.log(form.elements.password.type);
// → password
console.log(form.elements.name.form == form);
// → true
</script>
</body> </html>

6、阻止提交

然后自己用js提交(fetch,ajax等)

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<form action="example/submit.html">
Value: <input type="text" name="value">
<button type="submit">Save</button>
</form>
<script>
let form = document.querySelector("form");
form.addEventListener("submit", event => {
console.log("Saving value", form.elements.value.value);
event.preventDefault();
});
</script>
</body> </html>

7、快速插入单词

借助selectionStart和selectionEnd以及value属性:

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<textarea></textarea>
<script>
let textarea = document.querySelector("textarea");
textarea.addEventListener("keydown", event => {
// The key code for F2 happens to be 113
if(event.keyCode == 113) {
replaceSelection(textarea, "Khasekhemwy");
event.preventDefault();
}
}); function replaceSelection(field, word) {
let from = field.selectionStart,
to = field.selectionEnd;
field.value = field.value.slice(0, from) + word +
field.value.slice(to);
// Put the cursor after the word
field.selectionStart = from + word.length;
field.selectionEnd = from + word.length;
}
</script>
</body> </html>

8、实时统计字数

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<input type="text"> length: <span id="length">0</span>
<script>
let text = document.querySelector("input");
let output = document.querySelector("#length");
text.addEventListener("input", () => {
output.textContent = text.value.length;
});
</script>
</body> </html>

9、监听checkbox和radio

<label>
<input type="checkbox" id="purple"> Make this page purple
</label>
<script>
let checkbox = document.querySelector("#purple");
checkbox.addEventListener("change", () => {
document.body.style.background =
checkbox.checked ? "mediumpurple" : "";
});
</script>

X

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
Color:
<label>
<input type="radio" name="color" value="orange"> Orange
</label>
<label>
<input type="radio" name="color" value="lightgreen"> Green
</label>
<label>
<input type="radio" name="color" value="lightblue"> Blue
</label>
<script>
let buttons = document.querySelectorAll("[name=color]");
for(let button of Array.from(buttons)) {
button.addEventListener("change", () => {
document.body.style.background = button.value;
});
}
</script>
</body> </html>

10、监听select

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<select multiple>
<option value="1">0001</option>
<option value="2">0010</option>
<option value="4">0100</option>
<option value="8">1000</option>
</select> = <span id="output">0</span>
<script>
let select = document.querySelector("select");
let output = document.querySelector("#output");
select.addEventListener("change", () => {
let number = 0;
for(let option of Array.from(select.options)) {
if(option.selected) {
number += Number(option.value);
}
}
output.textContent = number;
});
</script>
</body> </html>

11、上传文件

<input type="file">
<script>
let input = document.querySelector("input");
input.addEventListener("change", () => {
if (input.files.length > 0) {
let file = input.files[0];
console.log("You chose", file.name);
if (file.type) console.log("It has type", file.type);
}
});
</script>

异步读内容:

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<input type="file" multiple>
<script>
let input = document.querySelector("input");
input.addEventListener("change", () => {
for(let file of Array.from(input.files)) {
let reader = new FileReader();
reader.addEventListener("load", () => {
console.log("File", file.name, "starts with",
reader.result.slice(0, 20));
});
reader.readAsText(file);
}
});
</script>
</body> </html>

12、本地存储数据

有时仅将数据保留在浏览器中就足够了。

localStorage。setItem(“username”,“marijn”);
控制台。日志(localStorage的。的getItem(“用户名”));
//→marijn
localStorage。removeItem(“username”);

localStorage特点:容量小,不可跨域。

类似的是sessionStorage,关掉浏览器就没有了。

简单笔记本↓

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
Notes:
<select></select> <button>Add</button><br>
<textarea style="width: 100%"></textarea> <script>
let list = document.querySelector("select");
let note = document.querySelector("textarea"); let state; function setState(newState) {
list.textContent = "";
for(let name of Object.keys(newState.notes)) {
let option = document.createElement("option");
option.textContent = name;
if(newState.selected == name) option.selected = true;
list.appendChild(option);
}
note.value = newState.notes[newState.selected]; localStorage.setItem("Notes", JSON.stringify(newState));
state = newState;
}
setState(JSON.parse(localStorage.getItem("Notes")) || {
notes: {
"shopping list": "Carrots\nRaisins"
},
selected: "shopping list"
}); list.addEventListener("change", () => {
setState({
notes: state.notes,
selected: list.value
});
});
note.addEventListener("change", () => {
setState({
notes: Object.assign({}, state.notes, {
[state.selected]: note.value
}),
selected: state.selected
});
});
document.querySelector("button")
.addEventListener("click", () => {
let name = prompt("Note name");
if(name) setState({
notes: Object.assign({}, state.notes, {
[name]: ""
}),
selected: name
});
});
</script>
</body> </html>

Excercise

① Content negotiation

课本答案:

const url = "https://eloquentjavascript.net/author";
const types = ["text/plain",
"text/html",
"application/json",
"application/rainbows+unicorns"]; async function showTypes() {
for (let type of types) {
let resp = await fetch(url, {headers: {accept: type}});
console.log(`${type}: ${await resp.text()}\n`);
}
} showTypes();

---------------------------

② A JavaScript workbench

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<textarea id="code">return "hi";</textarea>
<button id="button">Run</button>
<pre id="output"></pre> <script>
// Your code here.
let run = document.querySelector("#button");
let outputNode = document.querySelector("#output");
run.addEventListener("click", event => {
let code = document.querySelector("#code").value;
let func = Function(code);
let result;
try {
result = func();
} catch (error) {
result = error;
}
outputNode.innerText = String(result);
});
</script>
</body> </html>

------------------- - -

③ Conway’s Game of Life

暂时省略

Eloquent JavaScript #13# HTTP and Forms的更多相关文章

  1. Eloquent JavaScript #10# Modules

    索引 Notes 背景问题 模块Modules 软件包Packages 简易模块 Evaluating data as code CommonJS modules ECMAScript modules ...

  2. Eloquent JavaScript #04# Objects and Arrays

    要点索引: JSON More ... 练习 1.补:js字符串的表达方式有三种: "" 和 '' 没什么区别,唯一区别在于 "" 中写 "要转义字符 ...

  3. Eloquent JavaScript #12# Handling Events

    索引 Notes onclick removeEventListener Event objects stopPropagation event.target Default actions Key ...

  4. Eloquent JavaScript #11# The Document Object Model

    索引 Notes js与html DOM 在DOM树中移动 在DOM中寻找元素 改变Document 创建节点 html元素属性 布局 style CSS选择器 动画 Exercises Build ...

  5. Eloquent JavaScript #09# Regular Expressions

    索引 Notes js创建正则表达式的两种方式 js正则匹配方式(1) 字符集合 重复匹配 分组(子表达式) js正则匹配方式(2) The Date class 匹配整个字符串 Choice pat ...

  6. Eloquent JavaScript #05# higher-order functions

    索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...

  7. Eloquent JavaScript #03# functions

    索引: let VS. var 定义函数的几种方式 more... 1.作者反复用的side effect side effect就是对世界造成的改变,例如说打印某些东西到屏幕,或者以某种方式改变机器 ...

  8. Eloquent JavaScript #02# program_structure

    第一章中作者介绍了各种值,但是这些独立的值是没有意义的,只有当值放在更大的框架的时候才会彰显它们的价值.所以第二章开始介绍程序结构. 1.var VS. let 以及 const 作者推荐用 let ...

  9. Eloquent JavaScript #01# values

    When action grows unprofitable, gather information; when information grows unprofitable, sleep.      ...

随机推荐

  1. logging日志模块的使用

    logging日志模块的使用 logging模块中有5个日志级别: debug 10 info 20 warning 30 error 40 critical 50 通常使用日志模块,是用字典进行配置 ...

  2. 2018-2019-1 20189221 《Linux内核原理与分析》第七周作业

    2018-2019-1 20189221 <Linux内核原理与分析>第七周作业 实验六 分析Linux内核创建一个新进程的过程 代码分析 task_struct: struct task ...

  3. Boolean数据类型

    boolean 数据类型 boolean 变量存储为 8位(1 个字节)的数值形式,但只能是 True 或是 False,可以把它看做是一个和1代替,并且一定要小写.boolean operate是指 ...

  4. cocos 简便斗地主发牌动画

    niuniuSend : function (int) { switch(int) { case 0: for(var i=0;i<5;i++){ var sp = new ccui.Image ...

  5. 第一章 HTML基本标签

    1.HTML:HTML:超文本标签语言(标签又称标记.元素).浏览器:“解释和执行”HTML源码的工具 (运行网页的工具APP).客户端:享受服务的计算机服务器:提供服务的计算机 2.基本框架(网页最 ...

  6. 去掉idea中竖线

    1.现象如下: 2.解决办法. 3.解决后如下:

  7. Beta冲刺阶段5.0

    1. 提供当天站立式会议照片一张 2. 每个人的工作 (有work item 的ID) 成员 昨天已完成的工作 今天计划完成的工作 工作中遇到的困难 具体贡献 郑晓丽 首页活动详情界面的美化 实现首页 ...

  8. python中使用rabbitmq消息中间件

    上周一直在研究zeromq,并且也实现了了zeromq在python和ruby之间的通信,但是如果是一个大型的企业级应用,对消息中间件的要求比较高,比如消息的持久化机制以及系统崩溃恢复等等需求,这个时 ...

  9. django-pagination配置出错处理

    是否有人出现这类错误: 首先确认几个修改处: setting.py添加 INSTALLED_APPS = ( # ... 'pagination', ) 添加中间件 MIDDLEWARE_CLASSE ...

  10. Orangegreenworks封装rpgmakermv

    You’ll get a zip file with a folder called “lib” and a file called greenworks.js. Put both of them o ...