我正在尝试访问先前单击的段落/元素。用户将点击一个段落,bg-color 从白色变为蓝色,视觉上这对用户意味着他们点击的 P 现在被选中了。当他们点击不同的段落时,之前选择的段落的背景颜色会从蓝色变回白色。
有没有办法选择以前点击的段落?最好不添加和删除类。显然我下面的代码不起作用,但我已经解释了我认为答案可能如何工作?
$('p').bind('click', function () {
//checks if an item was previously selected, if so, sets background back to white
$(previouslySelected).css({'backgroundColor':'white'})
//change the current background blue, then store this item for the next click
$(this).css({'backgroundColor':'blue'})
var previouslySelected = $(this)
})