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
1 Mysql5.6 1.1 Related parameters MySQL 5.6 adds ...
Table of contents I. Definition 2. Usage scenario...
This collection showcases a number of outstanding ...
It is difficult to find good image material websi...
1: Tag selector The tag selector is used for all ...
Use blockquote for long citations, q for short ci...
Table of contents Preface analyze Initial Renderi...
Servermanager startup connection database error R...
Table of contents As attribute and property value...
Preface In the development of actual projects, we...
This article shares the installation tutorial of ...
Nowadays, application development is basically se...
There is a project developed on Mac, and the pack...
When I used g++ to compile the cpp file for the f...
Table of contents Install sakila Index Scan Sort ...