1. this keyword
This in the global function points to the window object
function fn() { consolo.log(this); } fn() This in the tag event attribute points to the window object
<button onclick="fn()">Click me to try</button> function fn() { console.log(this) } This in the event attribute function points to the label of the current operation
<button id="btn">Click me to try</button> var btn = document.getElementById('btn'); btn.onclick = function() { console.log(this); } 2. Custom attributes
var btn = document.getElementById('btn'); btn.index = 1; 3. Comprehensive case 1: implementation of tab
var uli = document.querySelector('ul').querySelectorAll('li'); var oli = document.querySelector('ol').querySelectorAll('li');
for (var i = 0; i < uli.length; i++) { uli[i].addEventListener('click', function () { } ) }
uli[i].index = i;
for (var i = 0; i < uli.length; i++) { uli[i].index = i; uli[i].addEventListener('click', function () { for (var j = 0; j < uli.length; j++) { uli[j].className = ''; oli[j].className = ''; } this.className = 'current'; oli[this.index].className = 'current'; }) }
appendix<style> * { margin: 0; padding: 0; list-style: none; } .box { height: 250px; width: 300px; border: 2px solid; } ul { display: flex; justify-content: space-between; background: red; border-bottom: 2px #ccc; } ul li { float: left; width: 100px; color: #fff; cursor: pointer; height: 40px; line-height: 40px; text-align: center; } ul li.current { border-bottom: 5px green solid; } ol li { margin: 30px; display: none; } ol li.current { display: block; } </style> <div class="box"> <ul> <li class="current">First page</li> <li>Page 2</li> <li>Page 3</li> </ul> <ol> <li class="current">I am the first page</li> <li>I am the second page</li> <li>I am the third page</li> </ol> </div> SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to open port 8080 on Alibaba Cloud ECS server
>>: Practical record of handling MySQL automatic shutdown problems
Suppose a user management system where each perso...
Configuration steps 1. Check whether DNS is confi...
Table of contents 1. Basic Introduction to JavaSc...
Gtid + Mha + Binlog server configuration: 1: Test...
An optimization solution when a single MYSQL serv...
This article shares a blinds special effect imple...
Use CSS to modify the browser scroll bar style ::...
This article mainly introduces the principle and ...
background There is a requirement in the project ...
There are two types of Linux system time. (1) Cal...
The HTTP status code is a 3-digit code used to in...
Table of contents Phenomenon: Port usage: Spellin...
Docker installation Install dependency packages s...
Table of contents cause reason Introduction to NP...
Table of contents Effect display Code Link Key Co...