简体   繁体   English

我有一个正在尝试打开的课程,但无法正常工作

[英]I have a class that I am trying to toggle on but it is not working properly

I am trying to toggle a class onto one of my divs when I click on a button and for some reason the class will be added to the div but the actual CSS properties do not happen. 当我单击按钮时,我试图将一个类切换到我的一个div上,由于某种原因,该类将添加到div中,但实际的CSS属性不会发生。 The function that I am trying to run is at the bottom. 我尝试运行的功能在底部。

Here is my code: 这是我的代码:

HTML 的HTML

<div class="mainPage">
   <form class="homeButton" action="">
      <input type="submit" value="Click to see who has had a worse day!">
   </form>
</div>
<div class="textBox">
   <h3>On (fill date), this person had a really terrible day.</h3>
   <div class="badEvent">
      <p>
      </p>
   </div>
   <button>Close</button>
</div>

CSS 的CSS

.mainPage {
   background-color: #043D5D;
   color: white;
   text-align: center;
   position: relative;
   top: 0px;
   left: 0px;
   right: 0px;
   h2 {
      font-size: 4em;
   }
   p {
      font-size: 2em;
   }
   form {
      padding-bottom: 20px;
      input {
         width: 350px;
         height: 50px;
         border-radius: 10px;
         font-size: .95em;
         border: 1px solid #EB5055;
         background-color: #EB5055;
         cursor: pointer;
      }
   }
}


.replace {
   background-color: red;
}

JS JS

$(".homeButton").on("click", function(event){

   event.preventDefault();
   $(".textBox").toggleClass('.replace');

});

The thing is, with the statement: 事情是这样的:

$(".textBox").toggleClass('.replace');

you are actually toggling the class .replace and not replace . 您实际上是在切换类.replace而不是replace The . . is only for querying process. 仅用于查询过程。 The class name is still replace only. 类名仍然只能replace So, change to 因此,更改为

$(".textBox").toggleClass('replace');

and the code will work . 和代码将工作

Is that the entire stylesheet or did you cut it off for brevity? 是整个样式表还是为了简洁起见? If it's not cut off, check the last bracket that seems to be missing after .replace and remove the period from the toggleClass. 如果没有切断,请检查.replace之后似乎缺少的最后一个括号,并从toggleClass中删除句点。

$(".textBox").toggleClass('replace');

Change to this: 更改为此:

$(".homeButton").on("click", function(event){ // add event inside the brackets
    event.preventDefault();
    $(".textBox").toggleClass('replace'); // remove the . from ('.replace')
});

Change .replace to replace since you are already toggling a class. 更改.replacereplace因为您已经在切换课程。

In the click function add an event parameter since you are using event.preventDefault(); 由于您正在使用event.preventDefault();click函数中添加event参数event.preventDefault();

JSFiddle Demo JSFiddle演示

Your class .homeButton must be on your button, not in your form tag. 您的类.homeButton必须在按钮上,而不是在表单标签中。 Also, if you are not sending the form it's better just to use an anchor tag or a button tag without the form. 另外,如果您不发送表单,则最好仅使用锚标签或按钮标签而不使用表单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图在同一页面上有第二个 slider 但它不起作用 - I am trying to have a second slider on the same page but its not working 我正在尝试运行脚本,但是我执行的步骤不起作用。 我哪里出问题了? - I am trying to run a script but the steps I have taken are not working. Where have I gone wrong? 我正在尝试使用 jquery 在文本和图像之间切换 - i am trying to swap between text and image using jquery toggle 我正在尝试在 javascript 中制作一个简单的切换按钮 - I am trying to make a simple toggle button in javascript 我正在尝试通过 IsFormDisplayed function 切换我的 Boolean - I am trying to toggle my Boolean by IsFormDisplayed function 我试图让一个“+”图标变成一个基于 Bootstrap 切换的“-”图标 - I am trying to get a “+” icon to turn into a “-” icon based on a Bootstrap toggle 我正在尝试使用作为无法正常工作的ajax函数来检索数据 - I am trying to retrive data using as an ajax function which is not working properly 我正在尝试将华氏度转换为摄氏度,但它不起作用我做错了什么? - Javascript(需要有一个表格和一个清除按钮) - I am trying to convert Fahrenheit to Celsius but it's not working what am i doing wrong? - Javascript (needs to have a form and a clear button) 如何在我的代码中切换一个类,我做错了什么? - How to toggle a class in my code, and what am I doing wrong? 我对使用切换类更改图像背景的颜色感兴趣 - I am interested in using toggle class to change the color of the background of an image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM