[英]refactoring a code with inline JavaScript
I'm trying to refactor this w3schools for a toggleable tabs code because all of us know that using inline JavaScript is a very bad practice so I'm trying to sepearte them as much as possible so I selected the tablinks
and I added an event listener but I'm struggling with the city names (take a look to their code and you'll understand what I'm talking about) 我正在尝试重构这个w3schools以获得可切换的标签代码,因为我们所有人都知道使用内联JavaScript是一种非常糟糕的做法,所以我试图尽可能地分离它们,所以我选择了
tablinks
并添加了一个事件监听器但我正在努力与城市名称(看看他们的代码,你会明白我在说什么)
any help please and thank you in advance 任何帮助,请提前感谢您
HTML HTML
<!-- Tab links -->
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<!-- Tab content -->
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
CSS CSS
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons that are used to open the tab content */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
JavaScript JavaScript的
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
w3schools is a very good source of bad practice, because they often take many shortcuts while trying to highlight the one small example they are giving. w3schools是一个非常好的不良实践来源,因为他们经常采取许多捷径,同时试图突出他们给出的一个小例子。
In practice, you would solve this situation by using a data- attribute. 实际上,您可以使用数据属性来解决这种情况。 If you don't recognize some of the calls, please refer to some of these articles:
如果您不认识某些电话,请参阅以下部分内容:
// Wrap our code in an IIFE in order to avoid polluting the global namespace // and to facilitate faster garbage collection (function(){ // Preload queries for later use const tabs = document.querySelectorAll('.tablinks'); const content = document.querySelectorAll('.tabcontent'); // iterate tab to create content interaction tabs.forEach(f => // f will be the tab element in this loop // Assign click event to each tab f.addEventListener('click',function(){ // Locate any previously marked active tab element const prevActive = document.querySelector('.tablinks.active'); // If a previously marked element exists set its classname to default if(prevActive) prevActive.className = 'tablinks'; // Assign the currently clicked tab element the active class f.className = 'tablinks active'; // Iterate through the content to look for the data-attribute we used earlier content.forEach(c => { // c will be the content element in this loop // if the id of the element matches the data attribute from the tab then show the content c.style.display = c.id == f.getAttribute("data-city") ? "block" : "none" ; }) }) ); })();
.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons that are used to open the tab content */ .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ .tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
<div class="tab"> <button class="tablinks" data-city="London">London</button> <button class="tablinks" data-city="Paris">Paris</button> <button class="tablinks" data-city="Tokyo">Tokyo</button> </div> <!-- Tab content --> <div id="London" class="tabcontent"> <h3>London</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Paris</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>Tokyo</h3> <p>Tokyo is the capital of Japan.</p> </div>
<button id="London" class="tablinks">London</button>
var btns = document.querySelectorAll('.tab button')
btns.forEach((btn)=>{
btn.addEventListener('click', (event)=>{
openCity(event, btn.id, btn)
})
})
Give each button an id with the city name, then pass the id to the openCity function 为每个按钮指定一个带有城市名称的id,然后将id传递给openCity函数
You can pass the btn to your openCity function too 您也可以将btn传递给openCity函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.