1-1可视区域 屏幕信息

  1. var screenWidth =window.screen.availWidth;
  2. //获取可视区域的width
  3. var viewwidth=document.body.clientWidth;
  4. console.log(screenWidth)
  5. console.log(viewwidth)

1-2 内联事件 自定义属性名

  1. <button id="btn" data-aid="123456" onclick="go(event)">btn</button>
  2. <script>
  3. function go(event){
  4. console.log("hello world");
  5. console.log(event.target.dataset.aid)
  6. }
  7. </script>

1-3

  1. <!-- firstChild 获取第一个子节点
  2. firstElementChild 获取第一个元素子节点
  3. lastChild
  4. lastElementChild -->
  5. <!-- nextSibling
  6. nextElementSibling
  7. previousSibling
  8. previousElementSibling -->
  9. <div>hello</div>
  10. <ul id="app">
  11. <li>html</li>
  12. <li>css</li>
  13. </ul>
  14. <div>
  15. world
  16. </div>
  17. <script>
  18. var app=document.getElementById("app");
  19. console.log(app.nextElementSibling)
  20. </script>

1-4 classlist

  1. <!-- add(); addclass(); -->
  2. <!-- remove(); removeclass(); -->
  3. <p id="app">hello world</p>
  4. <button id="btn">移除class</button>
  5. <script>
  6. var app=document.getElementById("app");
  7. var btn=document.getElementById("btn");
  8. app.onclick=function(){
  9. this.classList.add("current")
  10. }
  11. btn.onclick=function(){
  12. app.classList.remove("current")
  13. }
  14. </script>

1-5

  1. <!-- toggle toggleclass();
  2. contains hasclass() 判断是否包含某个class -->
  3. <p id="app">hello world</p>
  4. <script>
  5. var app=document.getElementById("app");
  6. app.onclick=function(){
  7. if(this.classList.contains("current")){
  8. this.classList.remove("current")
  9. }else{
  10. this.classList.add("current")
  11. }
  12. // this.classList.toggle("current")
  13. }
  14. </script>

1-6 函数

  1. function go(a){
  2. console.log(a)
  3. }
  4. go(10,12);

1-7 argument

  1. function go(a){
  2. console.log(arguments)
  3. console.log(a);
  4. }
  5. go(10,12);

1-8 重载

  1. function go(a){
  2. console.log(a);
  3. }
  4. function go(a,b){
  5. console.log(a+b);
  6. }
  7. go(10);
  8. go(10,20)

1-9

  1. function go(){
  2. if(arguments.length==1){
  3. console.log(arguments[0])//10
  4. }else if(arguments.length==2){
  5. console.log(arguments[0]+arguments[1])//30
  6. }
  7. }
  8. go(10)
  9. go(10,20)

1-10 函数的返回值

  1. function go(){
  2. return "hello world";
  3. console.log("good")
  4. }
  5. console.log(go())//hello world

1-11 对象的方法

  1. var obj ={
  2. name:"meng",
  3. sayName:function(){
  4. console.log(this.name);//meng
  5. }
  6. }
  7. obj.sayName();

1-12 todolist

  1. <input type="text" id="input">
  2. <ul id="app">
  3. </ul>
  4. <script>
  5. // indexOf 判断数组下标 值为-1 不存在
  6. //var arr=["html","css"];
  7. //console.log(arr.indexOf("js")) -1
  8. var arr=[];
  9. var input=document.getElementById("input");
  10. var app=document.getElementById("app");
  11. input.onkeydown=function(event){
  12. if(event.keyCode==13){
  13. if(arr.indexOf(this.value)==-1 && this.value!="")
  14. arr.push(this.value);
  15. var li=document.createElement("li")
  16. li.innerHTML=this.value;
  17. app.append(li);
  18. }
  19. console.log(arr);//["tui ", "you", "he"] 输入空值及重复值不显示
  20. }
  21. </script>

1-13 data

  1. var obj=[
  2. {name:"cheng",age:18,skill:"html"},
  3. {name:"meng",age:21,skill:"css"},
  4. {name:"sheng",age:17,skill:"js"},
  5. ]
  6. var arr=[];
  7. for(var i=0;i<=obj.length;i++){
  8. var name = obj[i].name;
  9. var skill =obj[i].skill;
  10. arr.push({
  11. name:name,
  12. skill:skill
  13. })
  14. }
  15. console.log(arr);

1-19 解构

  1. var obj={
  2. name:"li",age:18,skill:"vue",s:{
  3. sex:"male"
  4. }
  5. };
  6. // var name=obj.name;
  7. // var age=obj.age;
  8. // var skill=obj.skill;
  9. var {name,s}=obj;
  10. console.log(name)
  11. console.log(s.sex)

1-20 tab

  1. style
  2. .current{
  3. color:red;
  4. }
  5. <ul>
  6. <li class="current">html</li>
  7. <li>css</li>
  8. </ul>
  9. <div class="parent">
  10. <div class="html">html</div>
  11. <div class="css">css</div>
  12. </div>
  13. <script>
  14. var lis=document.getElementsByTagName("li");
  15. var divs=document.querySelectorAll(".parent div");
  16. for(let i=0;i<lis.length;i++){
  17. lis[i].index=i;
  18. lis[i].onclick=function(){
  19. //遍历让所有的li移除class="current"
  20. for(let i=0;i<lis.length;i++){
  21. lis[i].classList.remove("current");
  22. }
  23. //让当前元素添加class="current"
  24. this.classList.add("current")
  25. console.log(this.index)
  26. /* 3.让所有的div隐藏 */
  27. for(let i=0;i<divs.length;i++){
  28. divs[i].style.display = "none"
  29. }
  30. /* 4.让对应的下标的div去显示 */
  31. divs[this.index].style.display = "block";
  32. }
  33. }
  34. </script>