我编写了这个函数来在 Font Awesome 箭头之间切换:
function toggleClass() {
$('.fa-arrow-up').on('click', function (event) {
$('.info-section').toggleClass('hidden');
$(this).addClass('fa-arrow-down').removeClass('fa-arrow-up')
// console.log($(this))
})
$('.fa-arrow-down').on('click', function (event) {
$('.info-section').toggleClass('hidden');
$(this).addClass('fa-arrow-up').removeClass('fa-arrow-down')
console.log($(this))
})
}
$(toggleClass);
第一次单击时,当类为 fa-arrow-up
时,它会起作用,当类为 fa-arrow-down
时,我第二次单击它,它会切换 .info-section
,但不会用 fa-arrow-up
替换 fa-arrow-down
。关于为什么这个功能不能双向工作有什么建议吗?
更新添加 HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<i class="fas fa-arrow-up" aria-hidden="false"></i>
<script src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/c79911a95b.js" crossorigin="anonymous"></script>
</body>
</html>
最佳答案
希望这对您有帮助。
$('.fa-arrow-up').on('click', function(event) {
$(this).toggleClass('fa-arrow-down fa-arrow-up')
})
<i class="fas fa-arrow-up" aria-hidden="false"></i>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://kit.fontawesome.com/c79911a95b.js"></script>
关于javascript - jQuery 不响应第二个 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60444373/