메뉴를 클릭하면 .active
클래스와 목록 항목 (단락) 내의 일부 내용이 추가됩니다. 활성 클래스를 추가하는 방법을 알아 냈지만 목록 항목에 더 이상 활성 클래스가 없으면 추가 내용을 제거하는 방법을 모르겠습니다.활성 요소에 내용을 추가하지만 요소가 활성 상태가 아니면 제거하고 있습니까?
<ul>
<li><a href="#nowhere" title="Lorum ipsum dolor sit amet">Lorem</a></li>
<li><a href="#nowhere" title="Aliquam tincidunt mauris eu risus">Aliquam</a></li>
<li><a href="#nowhere" title="Morbi in sem quis dui placerat ornare">Morbi</a></li>
<li><a href="#nowhere" title="Praesent dapibus, neque id cursus faucibus">Praesent</a></li>
<li><a href="#nowhere" title="Pellentesque fermentum dolor">Pellentesque</a></li>
</ul>
그리고 내 jQuery를 :
$("ul li").click(function(){
$(this).addClass("active").siblings().removeClass("active");
$(this).append(function(){
if ($(this).hasClass("active")) {
return "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
} else {
return false;
}
});
});
이것은 .active
가 추가 된 경우 원하는 단락을 추가하지만 목록 항목이 더 이상이 문제를 가지고 일단 제거하지 않습니다 여기에
왜 'hasClass ("active") 체크가 필요한가? 항상 위의 줄에 클래스를 추가 했으므로 다른 비활성 컨테이너에서'p'를 절대로 제거하지 않습니다. – tymeJV