[英]jquery toggle() adding and removing a class
I have created a simple jQuery toggle function which adds and removes the class formVisable
the toggle function is adding the class how in the wrong place CLICK HERE . 我创建了一个简单的jQuery切换功能,添加和删除类
formVisable
切换功能被添加在错误的地方类如何点击这里 。
I want to add the class to the following element <div id="hide-overlay">
however at the moment the class is being added to my button element <a id="show-form">
. 我想将类添加到以下元素
<div id="hide-overlay">
但是此刻该类正在添加到我的按钮元素<a id="show-form">
。 Below is a snippet of my code 以下是我的代码片段
HTML 的HTML
<div class="row promo-image responsive">
<figure>
<figcaption class="caption">
<a id="show-form" class="promo-button" href="#">
<span>Be More Productive</span>
<i class="fa fa-download"></i>
</a>
<h4>Download the 8 steps for successful collabration</h4>
</figcaption>
</figure>
</div>
HIDDEN ELEMENT 隐藏元素
<div id="hide-overlay">
<a id="hide-form-close" href=""><i class="fa fa-times fa-2x"></i></a>
<div class="col-sm-6 col-sm-offset-3 form-container">
<h2 class="business">Register</h2>
<form class="main-contact submit-fade ajax-form" action="process.php" method="POST">
<ul class="small-block-grid-2 medium-block-grid-2 hide-form">
<li>
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Name" required>
</li>
<li>
<label for="email">Email</label>
<input type="text" class="form-control" name="email" placeholder="Email">
</li>
</ul>
<input type="submit" class="btn btn-success">
</form>
</div>
</div>
JQUERY JQUERY
var $hideOverlay = $("#hideOverlay").hide();
$("#show-form").on("click", function(e){
$(this).toggleClass("formVisable");
});
It's because you are using this
which represents the object that is calling the function, in this case #show-form
. 这是因为你使用
this
表示正在调用该函数的对象,在这种情况下#show-form
。 You should use $('#hide-overlay').toggleClass("formVisible");
您应该使用
$('#hide-overlay').toggleClass("formVisible");
Here's the fiddle for it: http://jsfiddle.net/norg5k2o/4/ 这是它的小提琴: http : //jsfiddle.net/norg5k2o/4/
"This" represents the object that an action is being applied to. “ This”表示要对其执行操作的对象。 In this case, it's your button.
在这种情况下,这是您的按钮。 You want the class to be applied to your overlay, rather than to the button.
您希望将该类应用于您的叠加层,而不是应用于按钮。 Therefore, try this:
因此,请尝试以下操作:
Also, you had some spelling errors. 另外,您还有一些拼写错误。
$("#hide-overlay").hide()
$("#show-form").on("click", function(e){
$("#hide-overlay").toggleClass("formVisible");
});
The $(this)
inside a JQuery event handler is the DOM element that has the handler attached. JQuery事件处理程序中的
$(this)
是附加了处理程序的DOM元素。 If we attached the handler to something else, like the containing <div>
, we will get the DIV DOM element doesn't matter which inside elements of the DIV we clicked. 如果将处理程序附加到其他内容,例如包含
<div>
,则将获得DIV DOM元素,与单击DIV的哪个内部元素无关紧要。
So, in this case you would need something like this, since $("#hideOverlay")
is already cached, the correct way to do it is: 因此,在这种情况下,您将需要这样的内容,因为
$("#hideOverlay")
已被缓存,正确的方法是:
var $hideOverlay = $("#hideOverlay").hide();
$("#show-form").click(function(e){
$hideOverlay.toggleClass("formVisible");
//Prevent default behavior for links and buttons
e.preventDefault();
});
You did not properlay declare the variable. 您没有适当地声明变量。 You had
$(#hideOverlay)
when your HTML id attribute was actually id='hide-overlay'
. 当您的HTML id属性实际上是
id='hide-overlay'
时,您有$(#hideOverlay)
。 As such, I have re-written the jQuery to properly target and hide the form as was your original intent. 因此,我重新编写了jQuery以正确定位并隐藏表单,这正是您的初衷。 I also, disabled the link so that it will only do the show/hide function and not the go to href='#' action.
我也禁用了链接,以便仅执行显示/隐藏功能,而不会执行href ='#'操作。
As the other answers suggest, when using the $(this) selector in jQuery, it will target the parent selector of the current iteration of the function. 正如其他答案所暗示的那样,当在jQuery中使用$(this)选择器时,它将针对函数当前迭代的父选择器。
I also updated your CSS as it was hiding the form. 我还更新了您的CSS,因为它隐藏了表单。
var $hideOverlay = $("div#hide-overlay");
$hideOverlay.toggle();
$hideOverlay.css("border", "solid 1px black");
$("a#show-form").on("click", function(e){
e.preventDefault();
$hideOverlay.slideToggle(300);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.