2012-07-24 2 views
0

아래 코드의 스왑 부분은 정상적으로 작동하지만 재설정 부분에서 문제가 발생합니다. 내가 원하는 것은 목록에있는 이미지 중 하나를 클릭하면 모두가 _off 이미지로 돌아간 다음 클릭 한 이미지가 _over로 전환됩니다.jQuery Image Swap - 다른 모든 이미지 재설정

미리 감사드립니다.

// Reset 
$('.show').attr('src').replace("_over","_off"); 

// Swap       
if($(this).attr("class") == "show") { 
    this.src = this.src.replace("_off","_over"); 
} else { 
    this.src = this.src.replace("_over","_off"); 
} 

답변

0

.attr에서 반환 된 값을 변경하면 요소가 변경되지 않습니다. 이 둘 이상있는 경우

$('.show')[0].src.replace("_over","_off"); 

,

$('.show').each(function() { 
    this.src.replace("_over","_off"); 
}); 
2

당신은 그것을 할 단지 수 있습니다 다음 show 클래스를 가진 항목이 하나 인 경우

: 따라서, 다음 중 하나 무엇인가 이미지 클릭 이벤트 핸들러를 바인딩 할 때 알아야 할 것은 현재 클릭 된 'img'요소가 무엇인지 알아야하기 때문입니다.

$('img').click(function(){ // <-- when img is clicked 
    $(this).attr('src','_over'); // <-- change current clicked img   src = _over 
    $('img').not(this).attr('src','_off'); // <-- change all other img to  src= _off 
}); 

http://jsfiddle.net/Tkp4E/