【问题标题】:jquery remove class from divjquery从div中删除类
【发布时间】:2016-05-21 01:49:58
【问题描述】:

我正在尝试从 DIV 中删除一个类,但我所能做的就是在我要删除的类旁边添加一个新类。如何彻底删除fullpage-wrapper

<script>
  jQuery('#productPage').addClass('test3-test').removeClass('fullpage-wrapper');
</script>

【问题讨论】:

  • 如果你只用removeClass(name),你能显示前后吗?
  • 将此脚本添加到 $(function(){//your code});
  • 会有很多问题,例如,您将代码包装到 dom 就绪块中吗?您正确链接了 jquery 库吗?
  • 你能创建一个 jsFiddle 来显示这个问题吗?
  • 也许您在执行该代码后的某处添加了类 fullpage-wrapper

标签: javascript jquery html class


【解决方案1】:

只是添加了一些cmets来解释程序

$(document).ready(function(){
    $("#add").click(function(){
        $("#demo").addClass("demo"); //add class "demo" to div "demo"
    });

   $("#remove").click(function(){
        $("#demo").removeClass("demo"); //remove class "demo" from div "demo"
    });
});
.demo{
  background: red;
height:50px;
width:250px;
  }
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

</head>
<body>

<div id="demo">This is a demo</div>

<button id="add">Add Class</button>
<button id="remove">Remove Class</button>

</body>
</html>

【讨论】:

  • 这是什么?用一些文字来解释你的答案,而不仅仅是代码转储,怎么样?
  • 我不确定将单行 add+remove 更改为两个单独的点击处理程序是否与问题有很大关系。他们清楚地知道addClass()removeClass() 存在。
【解决方案2】:

我认为你必须把你的js代码放在$(document).ready(function(){// here your js code});)

这样,您的 js 代码将在您的 html 页面加载后执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2010-10-31
    • 2013-04-26
    相关资源
    最近更新 更多