简体   繁体   English

选择在jQuery中动态添加的类

[英]Selecting class that was added dynamically in jquery

i'm trying to make changes to a div with a class name added with addClass(). 我正在尝试使用添加了addClass()的类名对div进行更改。 but i can't get it work. 但我不能使它工作。

the div. div。 clicking this div would remove "one" from the class, and replace it with "two", which i've managed to do. 单击此div将从类中删除“一个”,并将其替换为“两个”,这是我设法做到的。

<div class="product one">
    Click
</div>   

and the jquery. 和jQuery。 unfortunately, i cant seem to remove "two" and add "three" or apply any changes on jquery to "two". 不幸的是,我似乎无法删除“两个”并添加“三个”或将对jquery的任何更改应用到“两个”。

$('.product').on('click', function() {
    $(this).removeClass('one').addClass('two');
    TweenLite.to(".home_loop", 0.3, {height:110});
});

$('.two').on('click', function() {
    $('this').removeClass('two').addClass('three');
    TweenLite.to(".home_loop", 0.3, {height:0}); 
});

here's a simplified example (with the tweenlite stripped out) used an inspector to see the class changes when i click on the words "Click". 这是一个简化的示例(去除了Tweenlite),当我单击“ Click”一词时,使用了检查器来查看类的变化。

http://jsfiddle.net/do6t4pa5/3/show/ http://jsfiddle.net/do6t4pa5/3/show/

also i tried using .toggle but it will make the link in the div not work. 我也尝试使用.toggle,但它会使div中的链接无法正常工作。

Try the FIDDLE 尝试FIDDLE

Code for reference 参考代码

$(document).ready(function(){
$('.product').on('click', function() {
    $(this).removeClass('one').addClass('two');
    $('.two').on('click', function() {
    $(this).removeClass('two').addClass('three');
});
});
});

hope it helps 希望能帮助到你

$(document).ready(function(){

}).on('click','.product',function(){
    $(this).removeClass('one').addClass('two');
}).on('click','.two',function(){
    $(this).removeClass('two').addClass('three');
});

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

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