简体   繁体   English

.click没有注册更改的类

[英].click not registering changed class

I am trying to create a script that will change the class of another element to avoid the script from being activated under the new class, however, it is not registering with the script, the script considers the old class to still be there. 我正在尝试创建一个脚本,它将更改另一个元素的类,以避免在新类下激活脚本,但是,它没有注册脚本,脚本认为旧类仍然在那里。

<script>
$(document).ready(function(){
 $(".element1").click(function(){
  $(".element1").addClass("clicked");
  $(".element1").removeClass("element1");
});});
</script>

Current code is above with what i am trying to achieve. 目前的代码高于我想要实现的目标。

The element(s) that has class element1 was binded to a click handler on load... 具有类element1的元素在加载时被绑定到单击处理程序...

Even if you remove the class from it, it still is binded to the event handler. 即使您从中删除了类,它仍然绑定到事件处理程序。
As you can see below: 如下所示:

 $(document).ready(function(){ $(".element1").click(function(){ console.log("click"); $(".element1").addClass("clicked"); $(".element1").removeClass("element1"); }); }); 
 .element1{ color:blue; } .clicked{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="element1">element 1</div> 

I suggest you to use off() like this: 我建议你像这样使用off()
Thanks to Dashtinejad for mentionning that .unbind() is deprecated as of jQuery 3.0... Works the same. 感谢Dashtinejad提到.unbind()从jQuery 3.0开始被弃用......工作方式相同。

 $(document).ready(function(){ $(".element1").click(function(){ console.log("click"); $(this).off("click").addClass("clicked").removeClass("element1"); }); }); 
 .element1{ color:blue; } .clicked{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="element1">Element 1</div> 

And use chaining... 并使用链接......
;) ;)

Try this, you can achieve it by using onclick of particular element. 试试这个,你可以通过使用特定元素的onclick来实现它。 Here the on click listener is binding to the ".element1" in the document at the time of clicking. 这里的点击监听器在点击时绑定到文档中的“.element1”。

if you want to execute a click event only one time then you can also use .one() 如果你只想执行一次点击事件,那么你也可以使用.one()

$(".element1").one("click",function(){ 
    <!-- your code here -->
});

 $(document).ready(function() { $(document).on("click", ".element1", function() { console.log("click"); $(".element1").addClass("clicked"); $(".element1").removeClass("element1"); }); }); 
 .element1 { width: 100px; height: 100px; background-color: green; } .clicked { width: 100px; height: 100px; background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div1" class="element1"></div> 

my solution is: clicked in the div 我的解决方案是: 点击div

see one(), off(), on(); 见一(),关(),开(); in : 在:

http://www.w3schools.com/jquery/event_one.asp http://www.w3schools.com/jquery/event_one.asp

http://www.w3schools.com/jquery/event_on.asp http://www.w3schools.com/jquery/event_on.asp

http://www.w3schools.com/jquery/event_off.asp http://www.w3schools.com/jquery/event_off.asp

 $(document).ready(function(){ $(".element1").one("click", function() { console.log("is clicked"); $(".element1").addClass("clicked").removeClass("element1"); }); }); 
 .element1{ background-color:red; } .clicked{ background-color:blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="element1">hola</div> 

Do like this 这样做

$(document).ready(function() {
  $(".element1").click(function() {
    $(this).addClass("clicked").removeClass("element1");
  });
});

Note here jquery method-chaining is used. 注意这里使用jquery方法链接 You dont need to select the element multiple time to add method to it 您不需要多次选择元素来添加方法

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM