2013-06-14 5 views
1

내 페이지에 여러 개의 이미지가 있습니다. 일부는 스크롤을 필요로하는 페이지의 맨 아래에 있습니다. 내가 찾는 것은 이미지를 클릭하면 비디오로 바뀌어 페이지 상단으로 이동한다는 것입니다.jquery click 페이지 상단으로 이동하지 못하도록

$('#videoconner').click(function() { 
    $(this).replaceWith('<iframe width="280" height="164" src="http://www.youtube.com/embed/6kR2n4344UCAU?autoplay=1&cc_load_policy=1&rel=0" frameborder="0" allowfullscreen></iframe>'); 
}); 

내가 추가 : 여기

는 내가 가지고있는 코드입니다

e.preventDefault(); 

하지만 도움이되지 않았다.

어쨌든 페이지 상단으로 이동하지 못하도록 차단할 수 있습니까?

+0

'e.preventDefault();'를 수행하는 동안 함수 브래킷에'e'가 있습니까? 'function (e) {e.preventDefault()}' –

답변

3

e (이벤트 매개 변수 용)을 클릭 기능에 추가하십시오.

$('#videoconner').click(function (e) { 
    e.preventDefault(); 
    $(this).replaceWith('<iframe width="280" height="164" src="http://www.youtube.com/embed/6kR2n4344UCAU?autoplay=1&cc_load_policy=1&rel=0" frameborder="0" allowfullscreen></iframe>'); 
}); 
0

당신은

$('#videoconner').click(function (e) { 
    e.preventDefault(); 
    $(this).replaceWith('<iframe width="280" height="164" src="http://www.youtube.com/embed/6kR2n4344UCAU?autoplay=1&cc_load_policy=1&rel=0" frameborder="0" allowfullscreen></iframe>'); 
}); 
2

당신은 false를 반환 할 (이 콜백 함수 매개 변수에 e를 추가 )이 같은 preventDefault를 사용해야합니다; event.preventDefault() vs. return false

참고 : 거짓은 전자를 호출 반환

$('#videoconner').click(function() { 
    $(this).replaceWith('<iframe width="280" height="164" src="http://www.youtube.com/embed/6kR2n4344UCAU?autoplay=1&cc_load_policy=1&rel=0" frameborder="0" allowfullscreen></iframe>'); 
    return false; 
}); 

이 주제에 대한 자세한 정보를 원하시면이 읽기 : 그것은 거품까지 DOM을에 계속 기본 동작 & 일에서 클릭 이벤트를 중지 만들려면 .stopPropagation이 질문처럼 jQuery를 사용하는 경우.

관련 문제