老师,为什么在.prev()和.next()前面加 $(this)才生效,谢谢
来源:2-5 编程练习
qq_慕移动3101913
2020-03-20 19:14:39
<script>
/*此处写代码*/
$(document).ready(function () {
$('li').on('click',function () {
$(this).css('background-color','yellow').siblings().css('background-color','white');
$(this).prev().css('background-color','blue');
$(this).next().css('background-color','red');
})
})
</script>
老师,为什么在.prev()和.next()前面加 $(this)才生效。
1回答
同学你好,prev()和next()是获取选中元素的上一个元素和下一个元素,只有选中一个元素才能知道它上一个和下一个元素是什么。this指向函数的调用者,即点击哪一个li,this指向的就是哪一个li 。所以在方法前面加上$(this),找的就是当前点击li的上一个和下一个元素。
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
相似问题