လုပ်နည်း - Active Class ကို Current Element သို့ ထည့်နည်း
JavaScript ဖြင့် လက်ရှိဒြပ်စင်သို့ အသက်ဝင်သော အတန်းကို ထည့်သွင်းနည်းကို လေ့လာပါ။
လက်ရှိ (နှိပ်ထားသည်) ခလုတ်ကို မီးမောင်းထိုးပြပါ-
တက်ကြွသောဒြပ်စင်
အဆင့် 1) HTML ကိုထည့်ပါ
ဥပမာ
<div id="myDIV">
<button class="btn">1</button>
<button
class="btn active">2</button>
<button class="btn">3</button>
<button class="btn">4</button>
<button class="btn">5</button>
</div>
အဆင့် 2) CSS ကိုထည့်ပါ
ဥပမာ
/* Style the buttons */
.btn {
border: none;
outline:
none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
}
/* Style the active class (and buttons on
mouse-over) */
.active, .btn:hover {
background-color: #666;
color: white;
}
အဆင့် 3) JavaScript ကိုထည့်ပါ။
ဥပမာ
// Get the container element
var btnContainer = document.getElementById("myDIV");
// Get all buttons with class="btn" inside the container
var btns =
btnContainer.getElementsByClassName("btn");
// Loop through
the buttons and add the active class to the current/clicked button
for (var i = 0; i < btns.length;
i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
သင့်တွင် စတင်ရန် ခလုတ်ဒြပ်စင်တွင် အသုံးပြုနေသော အတန်းအစား မရှိပါက၊ အောက်ပါ ကုဒ်ကို အသုံးပြုပါ။
ဥပမာ
// Get the container element
var btnContainer = document.getElementById("myDIV");
// Get all buttons with class="btn" inside the container
var btns =
btnContainer.getElementsByClassName("btn");
// Loop through
the buttons and add the active class to the current/clicked button
for (var i = 0; i < btns.length;
i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
// If there's no active class
if (current.length > 0) {
current[0].className = current[0].className.replace(" active", "");
}
// Add the active class to the current/clicked
button
this.className += " active";
});
}