基本上,我有 4 个列表。无论单击哪个列表,我都想添加一个类,如果单击了其他列表,我想从其他列表中删除该类。只想将其应用于已单击的那个。
<script>
$(document).ready(function(){
$("#li1").click(function(){
document.getElementById('li1').classList.add('focus');
document.getElementById('li2').classList.remove('focus');
document.getElementById('li3').classList.remove('focus');
document.getElementById('li4').classList.remove('focus');
});
$("#li2").click(function(){
document.getElementById('li2').classList.add('focus')
document.getElementById('li1').classList.remove('focus');
document.getElementById('li3').classList.remove('focus');
document.getElementById('li4').classList.remove('focus');
});
$("#li3").click(function(){
document.getElementById('li3').classList.add('focus')
document.getElementById('li1').classList.remove('focus');
document.getElementById('li2').classList.remove('focus');
document.getElementById('li4').classList.remove('focus');
});
$("#li4").click(function(){
document.getElementById('li4').classList.add('focus')
document.getElementById('li1').classList.remove('focus');
document.getElementById('li2').classList.remove('focus');
document.getElementById('li3').classList.remove('focus');
});
});
</script>
---HTML----
<ul>
<li id="li1"> some text here </li>
<li id="li2"> some text here </li>
<li id="li3"> some text here </li>
<li id="li4"> some text here </li>
</ul>