2015-01-08 1 views
1

http://www.example.com/blahblah/#fragment과 같은 페이지가 있다고 가정합니다. 검색 주소창에 직접 입력하면 브라우저가 페이지를로드하고 자동으로 #fragment 앵커로 이동합니다.조각 식별자가있는 URL을 열 때 기본 점프를 방지 하시겠습니까?

HTML 코드에 개입하지 않고 JavaScript로이 동작을 방지 할 수있는 방법이 있습니까? 어쩌면 브라우저가 앵커로 점프하기 바로 전에 발생하는 이벤트가있을 수 있습니까? 대문자 인 경우 링크를 클릭 한 후 점프가 아닌 직접 입력을 통해 페이지를 여는 경우입니다.

또한 에서 점프하는 것을 막으려 고합니다. 브라우저를 건너 뛰고 뒤로 스크롤하지 않으려면으로 지정하십시오. 해시에 텍스트를 앞에 추가, 그래서 더 이상 기존 요소를 참조 -

+0

[페이지 스크롤링없이 document.location.hash 수정]의 중복 가능성 (http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling) –

답변

0

나는 당신과 같은 몇 가지 트릭을 할 필요가 당신이 간단한 솔루션을 위해 Modifying document.location.hash without page scrolling

에 따라해야한다고 생각하지 않습니다!

위의 링크 중 하나의 답변에서 복사 한 코드 조각에서 가능한 힌트를 찾을 수 있습니다.

$(function(){ 
//This emulates a click on the correct button on page load 
if(document.location.hash){ 
$("#buttons li a").removeClass('selected'); 
s=$(document.location.hash.replace("btn_","")).addClass('selected').attr("href").replace("javascript:",""); 
eval(s); 
} 

//Click a button to change the hash 
$("#buttons li a").click(function(){ 
     $("#buttons li a").removeClass('selected'); 
     $(this).addClass('selected'); 
     document.location.hash="btn_"+$(this).attr("id") 
     //return false; 
}); 
}); 
관련 문제