2012-11-29 3 views
0

나는 그때 내가 addClass('overflow'}를 사용하는 클래스addClass ('overflow')하는 방법;

.overflow{overflow-y:scroll}; 

을 추가 할 수 있지만 클릭에 전체 페이지를 다시로드 버튼을 클릭합니다. 이 removeClass('overflow') 내가 .css('overflow','hidden')'auto','scroll','hidden' 때문에 사용을 선택하지 않을 것입니다 작업 후

이 나에게 적합하지 않습니다, 나는 그것이 완전히 사용 후 삭제하고 싶습니다. 페이지를 방지하기 위해

+0

는 관련 코드를 제시해주십시오. 어쩌면 [피들] (http://jsfiddle.net)에서. 버튼이 페이지를 다시로드하는 이유는 명확하지 않습니다. –

답변

2

왜 그냥 <a>를 사용 해달라고 href="#"?

페이지를 다시로드하지 않아도 스크립트를 실행할 수 있습니다. 을 yor에서

유 사소한 오타가 코드를 게시 : 당신은 }으로 addClass() 종료 ... 이것은 올바른 코드가 될 것이다 :

$("#targetElement").addClass('overflow');

+1

'href = "javascript : void (0);"'를 사용하는 것이 더 좋습니다. 그렇지 않으면 클릭이 맨 위로 스크롤됩니다. – Matanya

2

다시로드합니다 :

$("#yourbuttonid").click(function(e){ 
    e.preventDefault(); // this will prevent the link to be followed 
    //the rest of your code 
}); 
1

, 당신은 prevent default 앵커 click 이벤트가해야 페이지를 다시로드를 방지하기 위해 :

$("a.button").on("click", function(e) { 
    // ... addClass("overflow"); 

    e.preventDefault(); // or instead you may use 
         // return false; 
}); 
0
$("#yourbuttonid").click(function(e){ 

    //your code 

    e.preventDefault(); // this will prevent the link's default action 
         // make sure it comes last in your code, 
         // if not it will cancel your code from executing. 

});