에서 선택한 리튬 항목의 배경 색상을 변경할 수 없습니다 나는 는 UL
가 어떻게 내 UL 목록에서 선택 LI 항목의 (jQuery를 사용하여) 배경을 변경할 수 있습니다, 여러분의 도움이 필요합니다. 지금 당장은 내 목록의 전체 배경이 변경됩니다. 마지막으로주의해야 할 점은 새 LI 항목을 선택해야 색이 흰색으로 지정되어야한다는 것입니다.<!DOCTYPE html>
<html>
<script src="jquery.min.js"></script>
<head>
<style type="text/css">
#container {
width: 200px;
}
#list1 {
border: 1px solid red;
width: 100%;
}
#list1 ul {
margin: 0;
padding: 3px;
list-style-type: none;
}
#list1 li {
cursor: default;
}
.selected {
background: blue;
}
</style>
<script type="text/javascript">
window.onload = function() {
$('#list1').click(function(){
var that = $(this);
that.closest('li').find('.selected').removeClass('selected');
that.addClass('selected');
});
}
</script>
</head>
<body>
<div id="container">
<div id="list1">
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</div>
</body>
</html>
클릭 이벤트 처리기를 상위 항목이 아닌 목록 항목에 연결합니다. 'that.addClass' 'selected ')'는 항상 부모 div에 클래스를 추가합니다. – j08691