Javascript 如何添加类(溢出);

Javascript 如何添加类(溢出);,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想点击一个按钮,然后它添加类 .overflow{overflow-y:scroll}; 我使用了addClass('overflow'),但它会在单击时重新加载整个页面 在一个操作之后,它将removeClass('overflow') 我不会选择使用.css('overflow','hidden'),因为'auto','scroll','hidden'不适合我,我希望在使用后将其完全删除。为了防止重新加载页面: $("#yourbuttonid").click(function(e){

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

.overflow{overflow-y:scroll};
我使用了
addClass('overflow')
,但它会在单击时重新加载整个页面

在一个操作之后,它将
removeClass('overflow')

我不会选择使用
.css('overflow','hidden')
,因为
'auto','scroll','hidden'
不适合我,我希望在使用后将其完全删除。

为了防止重新加载页面:

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

要防止重新加载页面,请执行以下操作:

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

为了防止页面重新加载,您应该锚定
单击
事件:

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

    e.preventDefault();  // or instead you may use
                         // return false;
});

为了防止页面重新加载,您应该锚定
单击
事件:

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

    e.preventDefault();  // or instead you may use
                         // return false;
});

为什么不直接使用
href=“#”

这将不会重新加载页面并仍然触发脚本

在你发布的代码中,你有一个小错误:你用
}
结束了
addClass()
。这将是正确的代码:


$(“#targetElement”).addClass('overflow');

为什么不将
href=“#”
一起使用呢

这将不会重新加载页面并仍然触发脚本

在你发布的代码中,你有一个小错误:你用
}
结束了
addClass()
。这将是正确的代码:


$(“#targetElement”).addClass('overflow');

请显示相关代码。可能在a中。不清楚为什么按钮会重新加载页面。请显示相关代码。可能在a中。不清楚为什么按钮会重新加载页面。最好使用
href=“javascript:void(0);”
,否则单击会滚回顶部最好使用
href=“javascript:void(0);”
,否则单击会滚回顶部