发表于: 2017-04-12 21:19:53

1 1209


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin

使用有限状态机来存放对象的不同状态---有限状态机的实例:交通灯

浏览器的本地存储

了解JS 调试技巧

明天计划的事情:(一定要写非常细致的内容) 

 优化任务234

遇到的问题:(遇到什么困难,怎么解决的) 

使用有限状态机来存放对象的不同状态-----有限状态机的实例:交通灯 https://zhuangyin8.github.io/Ptteng-Task/Demo/JavaScript%20Traffic%20Light.html


收获:(通过今天的学习,学到了什么知识)

随着浏览器的处理能力不断增强,越来越多的网站开始考虑将数据存储在「客户端」。本地存储的好处一是避免取回数据前页面一片空白,如果不需要最新数据也可以减少向服务端请求的次数,从而减少用户等待从服务器获取数据的时间,二是网络状态不佳时仍可以显示离线数据

chrome浏览器打开一个网页,进入开发者模式,点击Application,我们可以看到:

以上的 Local StorageSession StorageIndexedDBWeb SQLCookies,就是常用的本地存储其中几种。



调错时常用的功能有:
  • 代码格式化
       可以将被压缩的代码自动展开
  • 实时代码编辑
       可以在运行时动态改变 JS 代码,并且不需要刷新页面就可以看到效果,一般用这个实时的在代码里插 console.log
  • DOM 事件/XHR 断点
       可以针对 DOM 结构改变/属性改变/键盘鼠标事件 等下断点,直接断到事件的第一个 listener 函数上。还可以对 XHR 请求下断点,断到发起请求的那一行代码上
  • 调用栈分析
       这个非常常用,Scripts 面板下右上角的那一部分
  • 自动异常断点
       当代码执行出错时,可以自动断到出错的代码行上,直接分析出错时的运行时环境
  • 分析 HTTP 请求
       Network 面板下列出了所有的 HTTP 请求,可以很方便的查看请求内容、HTTP 头、请求时间等信息

以线上代码出 Bug 为例。一般上手第一步是使用代码格式化功能将被压缩的线上代码展开,然后开启自动异常断点,尝试重现 Bug。当 Bug 出现时代码会自动断到出错的那一行。然后通过调用栈分析结合控制台找到最开始出错的那个函数上。一般的 Bug 到这里就算找出来了,但是如果这个 Bug 是在事件回调函数或者 XHR 回调函数上,就得结合 DOM 事件断点和 XHR 断点 进一步往上找哪个函数出错。另外,如果是发给服务器请求没有得到正确的 response,可以通过 Network 面板查看请求的参数、Cookie、HTTP 头是否有误。
   另外,还可以通过 Charles/Nginx/Fiddler 等工具将远程 js 代码映射到自己的电脑上,免去了代码格式化的麻烦,还可以直接编辑。

还有些比较小的 Tips:
  • console.group/console.groupEnd 可以将 log 信息分组展示,看起来更舒服
  • console.warn/console.error 可以输出 warning 和 error log
  • element 面板右侧实时编辑的 css 样式,可以在 resource 面板里保存起来
  • 关于样式还有个技巧,Elements 面板右上角的 styles 栏,右侧有三个图标,功能依次是:根据当前元素创建一个 css 样式、模拟 :hover/:active 等伪类、颜色值类型选择。实用
  • resource 面板可以删除和修改 cookie/localstorage
  • scripts 面板下的 ctrl+o 可以快速跳转脚本文件,ctrl+shift+o 快速跳转函数
  • 右下角的齿轮图标里有个选项是开发者工具和网页左右分屏(Dock to the right,默认是上下分屏),宽屏必备
  • 接上,还有个选项是禁用 Cookit,必点
  • 接上,在选项面板里还可以手工编辑 user agent 和模拟触摸事件


进一步增强理解常用的三种方法选择html元素

These three methods are the most commonly used for selecting HTML elements:
//finds element by id 通过id选择元素
document.getElementById(id)
//finds elements by class name 通过类名选择元素
document.getElementsByClassName(name)
//finds elements by tag name 通过标签名选择元素
document.getElementsByTagName(name)

修改html页面内容
In the example below, the getElementById() method is used to select the element with id="demo" and change its content
:

   1. getElementById(“demo”)   方法选择html页面iddemo的元素


var elem = document.getElementById("demo");

elem.innerHTML = "Hello World!";


The example above assumes that the HTML contains an element with id="demo", for example <div id="demo"></div>.

The getElementsByClassName() method finds all elements by class name and returns them as an array
.
2. getElementsByClassName(“demo”) 方法选择html页面所有类名为demo的元素

For example, if our HTML page contained three elements with class="demo", the following code would return all those elements as an array:


   var arr =  document.getElementsByClassName("demo");
//accessing the second element 访问数组arr中第二个元素

arr[1].innerHTML = "Hi";


3.Similarly, the getElementsByTagName() method returns all of the elements of the specified tag name as an array.


getElementsByTagName(“p”) 方法选择所有的p标签元素。


The following example gets all paragraph elements of the page and changes their content:

element.childNodes returns an array of an element's child nodes.元素的childNodes属性返回该元素的所有子元素。

element.firstChild returns the first child node of an element.元素的firstchild属性返回该元素的第一个子元素。

element.lastChild returns the last child node of an element.元素的lastchild属性返回该元素的最后一个子元素。

element.hasChildNodes returns true if an element has any child nodes, otherwise false. 判断元素是否有子元素

element.nextSibling returns the next node at the same tree level.元素的 nextSibling 属性返回该元素的下一个同级元素。    

element.previousSibling returns the previous node at the same tree level.元素的 previousSibling 属性返回该元素的前一个同级元素。    

element.parentNode returns the parent node of an element.元素的 parentNode 属性返回该元素的父元素。



返回列表 返回列表
评论

    分享到