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. vue mounted中监听div的变化

    vue mounted中监听div的变化 <div style="width:200px;height:30px;background: #0e90d2" id=" ...

  2. 异常处理的捕捉:try{}catch(异常类 变量)finally{最终执行}

    可以对异常进行针对性处理的方式.try{ //需要被检查的异常 }catch(异常类  变量)//该变量用于接收发生的异常{ //处理异常的代码 }finally{ //一定会被执行的代码. }

  3. OpenResty安装(Centos7.2)

    下载.解压安装包 [root]# wget https://openresty.org/download/openresty-1.11.2.5.tar.gz 安装libpq.pcre.openssl ...

  4. 用v-if 来给不同筛选出来的todo添加不同的按钮

    凡是数据里面有属性a为2的 我就给它放1,2,3   3个按钮 ,有属性为3的就没得按钮 ,属性a为1的 就给它 13两个按钮 效果如下:

  5. SQL 、NoSQL数据库教程

    前言: 嗯,先说说数据库的分类吧,其实主要大的分类就是关系型数据库(SQL)和非关系型数据库(NoSQL); 实验楼上有常见的数据库教程,这里做一个整理,希望对你学习数据库方面的知识有所帮助: 关系型 ...

  6. MySQL数据类型--与MySQL零距离接触2-12主键约束

    定义一个主键,可以用PRIMARY KEY,也可以用KEY. 主键约束的字段禁止为空. 写入4条记录,查看它的自动编号: 自动编号确实是1 2 3 4 AUTO_INCREMENT字段必须定义为主键, ...

  7. python filter函数应用,过滤字符串

    >>> candidate = 'dade142.;!0142f[.,]ad' >>> filter(str.isdigit, candidate) #保留数字 ' ...

  8. spring 对jdbc的简化

    spring.xml <!-- 加载属性配置文件 --> <util:properties id="db" location="classpath:db ...

  9. equals和==的区别小结

    ==: == 比较的是变量(栈)内存中存放的对象的(堆)内存地址,用来判断两个对象的地址是否相同,即是否是指相同一个对象.比较的是真正意义上的指针操作. 1.比较的是操作符两端的操作数是否是同一个对象 ...

  10. python拼接变量、字符串的3种方法

    第一种,加号(“+”): print 'py'+'thon' # output python str = 'py' print str+'thon' # output python 第二种 ,空格: ...