我試了下面的代碼,但是沒有顯示“fa-xmark ”,而是顯示了“fa-bars”。
我想在我點(diǎn)擊“fa-bar”時(shí)顯示“fa-xmark”
const toggleBtn = document.querySelector('.toggle_btn')
const toggleBtnIcon = document.querySelector('.toggle_btn i')
const dropDownMenu = document.querySelector('.dropdown-menu')
toggleBtn.onclick = function() {
dropDownMenu.classList.toggle('open')
const isOpen = dropDownMenu.classList.contains('open')
toggleBtnIcon.classList = isOpen ?
'fa-solid fa-xmark' :
'fa-solid fa-bars'
}
<link rel="stylesheet" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<div class="dropdown-menu">
<li><a href="hero">Home</a></li>
<li><a href="hero">Task</a></li>
<li><a href="hero">File</a></li>
<li><a href="hero">About</a></li>
<li><a href="#" class="sign_btn">Signup</a></li>
</div>