• js笔记6


    1、函数都有返回值,人为return,返回什么就是什么,否则,他的返回值就是undefined

    而方法的本质也是函数,所以也有返回值

    document.getElementById()返回的是获取的标签

    document.getElementsClassName()返回的是一个数组集合

    document.getElementsTagName()返回的是一个数组集合

    选中一个元素修改他的内容 (上边三个)

    document.getElementsByClassName("name")[0].innerHTML="hahahh";这句话的意思

    是选中一个所有用class起一个名字的元素数组,并选中数组中的第一个叫做这个名字的标签,给它

    修改内容为hahahh。

    2、改变标签的属性    元素.属性名

    var pic=document.getElementsByTagName("img");

    pic[0].src="路径src.jpg''

    3、修改和添加css样式    元素.style.width(css属性名)="属性值";

    var pic=documentByTagName("img");

    pic[0].style.width="500px";

    4、事件:用户在事件中所触发的行为

    (1)点击 onclik

    (2)鼠标进入 onmouseenter  鼠标离开onmouseleave

    (3)鼠标移动 onmousemove  

    (4)鼠标悬浮 onmouseover   鼠标移除 onmouseout

    (5)鼠标按下 onmousedown   鼠标抬起 onmouseup

    (6)表单聚焦 onfocus  表单失去焦点 onblur

    表单内容修改  onchange

    (7)浏览器加载完成 onload

    这些事件使用时必须跟一个函数配合使用

    1)事件 将事件当做标签属性使用 <img src="xx.jpg"  onclik="alert(123)"/>

    2)通过事件绑定,将事件当成一个元素的属性

    特例:对于class,html中的class在js中是关键字,获取class这个属性时必须使用clssName来获取或修改

    pic[0].onlick=function(){

      pic[0].className="pic"

    }

    5、JS中的for循环没有关系都是互相独立的

    *解决10.24案例中的i和for匹配的问题

    方法一:给事件套用一个自调用函数

    for( var  i=0;i<arr.length;i++){

      (function(i){

            lis[i].onlick=function(){

                tupian.src=arr[i]

              }

      })

    }

    方法二:人为定义属性,将索引存在属性里,需要的时候调用属性(this)

    for( var  i=0,i<arr.length,i++){

      人为定义一个属性,来存储for循环的i

      lis[i].index=i;

      lis[i].onlick=function(){

        tupian.src=[this.index]

      }

    }

    *this 

    this是js的一个关键字,它是一个对象,一般用在函数里用于指向函数内部的关系

    div.onlick=function(){this}

    如果函数在定义的时候前面有,那么this就指向点前面的对象,如果函数定义的时候没有对象,那么this就是window

  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/panghexin/p/9877836.html
Copyright © 2020-2023  润新知