문제
는 방법을 알고 싶어요 제대로 쓰는 경우 문 jquery.나의 무리가 있 div 의 앵커에서 각 클릭하면 해당 확장 div 의 폭을 사용하여 toggleClass.이 작품만을 쓰고 싶어하는 경우 문는지 확인하는 경우 다른 div 의 같은 클래스에 적용되는 경우,그래서는 계약 div,확장 한 다음 다음 div(희망하는 말),내 코드는 지금까지:
HTML:
<div class="content-block">
<h2>Title</h2>
<p>Content...</p>
<a class="expand" href="#">Expand View</a>
</div>
<div class="content-block">
<h2>Title</h2>
<p>Content...</p>
<a class="expand" href="#">Expand View</a>
</div>
<div class="content-block">
<h2>Title</h2>
<p>Content...</p>
<a class="expand" href="#">Expand View</a>
</div>
<div class="content-block">
<h2>Title</h2>
<p>Content...</p>
<a class="expand" href="#">Expand View</a>
</div>
JQUERY:
$(document).ready(function(){
$('a.expand').click(function(){
$(this).parent('.content-block').toggleClass('wide');
});
});
해결책
당신이 할 수 있는 다음과 같이,필요한 경우 이 경우는 다음과 같습니다.
$(document).ready(function(){
$('a.expand').click(function(){
$(this).parent('.content-block').toggleClass('wide')
.siblings('.wide').removeClass('wide');
});
});
이지 전환 넓은 클래스에서는 부모,제거한 클래스에서는 모두 형제자매입니다.
다른 팁
No need for an if statement, just run removeClass()
on anything that has the wide
class applied to it:
$(document).ready(function(){
$('a.expand').click(function(){
$('.content-block.wide').removeClass('wide');
$(this).parent('.content-block').addClass('wide');
});
});
And for performance sake, just use addClass()
and removeClass()
instead of toggleClass()
You want to write something like this:
var otherExpanded = $('.content-block.wide');
if (otherExpanded.length)
//Do things
제휴하지 않습니다 StackOverflow