2013-08-06 3 views
1

클릭하면 이미지가 변경되고 다른 이미지를 클릭하면 원본 이미지로 다시 바뀝니다. 지금까지 제가 처음 클릭 할 때 이미지를 변경했지만 다음 부분을 알지 못합니다. 다른 이미지를 클릭하면 원본 이미지로 돌아갑니다. 이것이 내가 가진 것입니다 :클릭하면 이미지가 다른 이미지로 변경됩니다.

$(function() { 
    $('#two').click(function(){ 
     $("#imgtwo").attr('src',"resources/items/icon2blue.png"); 
    }); 
}); 

어떤 도움을 주실 수 있습니까? 감사!

답변

0

이미지를 클릭하면 이미지를 변경하고 다른 모든 이미지를 기본 이미지로 다시 설정합니다. 이렇게하면 주어진 이미지가 클릭 할 때마다 이미지가 변경되고 다른 모든 이미지는 기본값으로 다시 설정됩니다.

$('#one').click(function(){ 
    $("#imgone").attr('src',"resources/items/click-image.png"); 
    $("#imgtwo").attr('src',"resources/items/standard-image.png"); 
    $("#imgthree").attr('src',"resources/items/standard-image.png"); 
}); 
$('#two').click(function(){ 
    $("#imgone").attr('src',"resources/items/standard-image.png"); 
    $("#imgtwo").attr('src',"resources/items/click-image.png"); 
    $("#imgthree").attr('src',"resources/items/standard-image.png"); 
}); 
$('#three').click(function(){ 
    $("#imgone").attr('src',"resources/items/standard-image.png"); 
    $("#imgtwo").attr('src',"resources/items/standard-image.png"); 
    $("#imgthree").attr('src',"resources/items/click-image.png"); 
}); 

내 JS는 녹슬었지만 아이디어를 얻었습니다.

+0

안녕하세요 스캇 :

또는, 당신은 아마 다음과 같이 수행 할 수 있습니다 토글 이미지를 갖고 싶어? 죄송 합니다만 jquery에 익숙하지 않으므로 익숙하지 않습니다. 고맙습니다 :) – maccas28

+0

일부 코드가 업데이트되어 추가되었지만 완벽하지는 않을 수 있습니다. –

0
var imgArray = [ 
'http://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Brain_human_sagittal_section.svg/295px-Brain_human_sagittal_section.svg.png', 
'http://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Solanum_muricatum_Flower_and_Fruit.jpg/220px-Solanum_muricatum_Flower_and_Fruit.jpg' 
]; 

var counter = 0; 
document.getElementById('my_button').onclick = function() { 
    document.getElementById('mainImage').src = imgArray[counter % imgArray.length]; 
    counter += 1; 
} 

희망이

가 this.set 다른 ATTRIB 시도이 바이올린

http://jsfiddle.net/sornalingam/XyezG/

+0

왜 jQuery를 사용하지 않습니까? 원래 질문은 jQuery를 사용하는 것입니다. – Raptor

+0

또한 'counter'는 9,007199254740,992 번 이상 클릭하면 오버 플로우됩니다. 물론 마우스 버튼은 이전에 깨졌습니다. – Raptor

+0

@ShivanRaptor 감사합니다. – Backtrack

2
$(function() { 
    $('#two').click(function(){ 
     $('#imgtwo').data('targetsrc',$(this).attr('src')); 
     $("#imgtwo").attr('src',"resources/items/icon2blue.png"); 
    }); 


    $('#imgtwo').click(function(){ 
     $("#two").attr('src',$(this).data('targetsrc')); 
     $('#two').data('targetsrc',$(this).attr('src')); 
    }); 
}); 

을 시도하고

+0

'$ ('# imgtwo')'가''이고,'targetsrc'는 유효한 속성이 아닙니다. 이렇게하면 W3C 유효성 검사가 중단됩니다. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img – Raptor

+0

몇 가지 사항을 변경했습니다. 이것을 시도하십시오 – vipulsharma

0

당신이 사용할 수 있습니다에서 가치를 얻을 도움이 될 것입니다 접근 방식 :

$(function() { 
    var count = 0; 
    $('#two').click(function(){ 
     $("#imgtwo").attr('src',"resources/items/icon2blue.png"); 
    }); 
    $('#image2').click(function(){ // do it with other images 
     if(count < 4) { 
      count++; 
     } else { 
      // reset to original image 
      count = 0; 
      $("#imgtwo").attr('src',"resources/items/original_image.png"); 
     } 
    }); 
}); 

idanother_image 인 것으로 가정합니다. 내가 그렇게 어떻게,

$(function() { 
    var img_state = true; 
    $('#two').click(function(){ 
     if(img_state) { 
      $("#imgtwo").attr('src',"resources/items/icon2blue.png"); 
     } else { 
      $("#imgtwo").attr('src',"resources/items/another_image.png"); 
     } 
     img_state = !img_state; 
    }); 
}); 
+0

Shivan, 나는 다른 버튼이 클릭되었을 때 원래 이미지로 되돌아 가야합니다. 4 개의 이미지가 있으므로 3 개의 이미지를 클릭하면 4 번째 이미지가 원래 이미지로 되돌아갑니다. – maccas28

+0

솔루션의 첫 번째 부분은 사용자의 필요에 맞습니다. – Raptor

+0

두 개 이상의 이미지를 어떻게 식별합니까? 클릭하면 네 번째 이미지가 변경됩니까? 다른 이미지가 아닌 다른 이미지를 식별하는 '#another_image'가 없으므로 – maccas28

관련 문제