0

我想点击一个按钮然后它添加类

.overflow{overflow-y:scroll};

我用过addClass('overflow'},但点击后会重新加载整个页面。

一个动作后removeClass('overflow') 我不会选择使用.css('overflow','hidden'),因为'auto','scroll','hidden'不适合我,我希望它在使用后完全删除。

4

4 回答 4

2

为了防止页面被重新加载:

$("#yourbuttonid").click(function(e){
   e.preventDefault();  // this will prevent the link to be followed
   //the rest of your code
});
于 2012-11-29T14:43:42.133 回答
2

你为什么不只使用<a>with href="#"

那不会重新加载页面并仍然触发您的脚本。

在您发布的代码中,您有一个小错字:您以addClass()...结尾,}这将是正确的代码:

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

于 2012-11-29T15:10:41.260 回答
1

为了防止页面重新加载,您应该防止默认click事件:

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

    e.preventDefault();  // or instead you may use
                         // return false;
});
于 2012-11-29T14:43:48.713 回答
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.

});
于 2012-11-29T15:02:45.223 回答