1.点击事件

1-1更改元素内容

  1. <body>
  2. <button id="btn">btn</button>
  3. <script>
  4. var btn = document.getElementById("btn");
  5. btn.onclick = function(){
  6. // console.log("hello world")
  7. this.innerHTML = "change"
  8. }
  9. //this指向正在执行事件的当前对象
  10. </script>
  1. innerText 可以改变元素的文本内容 不识别html标签会直接显示 去除空格和换行
  2. innerHTML 可以改变元素的超文本内容 识别html标签 保留空格和换行
  3. 这两个属性是可读写的 可以获取元素里面的内容

1-2更改元素样式

  1. <script>
  2. var btn = document.getElementById("btn");
  3. btn.onclick = function(){
  4. this.innerHTML = "60";
  5. this.style.backgroundColor = "lightblue";
  6. this.style.color = "red";
  7. this.style.fontSize = "20px"
  8. }
  9. </script>
  10. this.style.xxx="";
    <ul>
        <li>html</li>
        <li>css</li>
        <li>javascript</li>
    </ul>
    <script>
        var lis = document.querySelectorAll("ul li");
        for(var i=0;i<lis.length;i++){
            lis[i].onclick = function(){

            this.style.display = "none";
            console.log(this);
        }
        }
    </script>

tips:只能对单个对象执行事件,不能对集合执行事件

image.png