일부 div를 확장/숨기기 위해 jQuery를 사용하고 있습니다. 링크를 클릭 할 때 해당 div를 열 수 있도록 이벤트 처리기를 추가해야합니다. 전환 된 각 div에는 고유 한 클래스가 할당됩니다. 이벤트 처리기를 작성하는 방법에 대한 조언을 찾고 있습니다.링크를 클릭 할 때 div를 확장하는 jQuery 이벤트 핸들러 추가하기
JQuery와
$(document).ready(function(){
$(".toggle_container:not(:first)").hide();
$(".toggle_container:first").show();
$("h6.trigger:first").addClass("active");
$("h6.trigger").click(function(){
$(this).toggleClass("active");
$(this).next(".toggle_container").slideToggle(300);
});
는 CSS :
// uses a background image with an on (+) and off (-) state stacked on top of each other
h6.trigger { background: url(buttonBG.gif) no-repeat;height: 46px;line-height: 46px;width: 300px;font-size: 2em;font-weight: normal;}
h6.trigger a {color: #fff;text-decoration: none; display: block;}
h6.active {background-position: left bottom;}
.toggle_container { overflow: hidden; }
.toggle_container .block {padding: 20px;}
HTML과 같은 링크 목록 가지고 열
<a href="#">One</a>
<a href="#">Two</a>
및 coorisponding div가 :
을<h6 class="trigger">Container one</h6>
<div class="toggle_container">
div one
</div>
<h6 class="trigger">Container two</h6>
<div class="toggle_container Open">
div one
</div>
앞서 언급했듯이이 작업을 용이하게 할 고유 한 클래스를 할당 할 것입니다.
어떤 조언이 필요합니까? 감사!
- 내가 링크가 인스턴스에 대한 탐색에서, 페이지의 다른 부분에서 클릭 할 때 특정 사업부를 개방 전환 이벤트 핸들러를 구축하기위한 몇 가지 조언을 찾고 있어요 명확합니다.
코드가 정상적으로 보입니다. 질문하는거야? – SLaks
'$ (".toggle_container : not (: first)"대신에'$ ("div.toggle_container"). 숨기기(). 실행 시간을 약 0.00001ms 줄여줍니다 ... 매우 까다 롭다면 : – Matt
죄송 합니다만, 분명히 명확하지 않습니다 (두뇌가 튀었습니다). $ (". toggle_container : first"). 더 명확하게 질문을 업데이트했습니다. 위의 코드는
을 클릭하면 효과적입니다. 어떻게 해야할지 알아 내고 싶습니다.이 div 토글 러에 다른 클래스를 할당하고 페이지의 다른 링크에서 여는 것입니다. – user279860