2017-06-22 123 views
-1

有两个标签,我希望他们旋转点击。我用JQuery来做到这一点,但它只适用于第一次点击! 我怎样才能使它在每次点击上都有效?Jquery-添加css onclick只在第一次点击时工作

$(document).ready(function() { 
 
    $(document).on("click", "#male", function() { 
 
    $('#male i').css({ 
 
     'transform': 'rotate(360deg)' 
 
    }) 
 
    }) 
 
});
#male i, 
 
#female i { 
 
    transition: 1s; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script> 
 
<label id="male" class="pull-left text-center col-md-5 btn btn-default" style="height: 36px;"> 
 
    <i class="fa fa-2x fa-male text-center" style="color: tomato"> 
 
    <input type="radio" name="gender" value="male" checked> 
 
    </i> 
 
</label> 
 
<label id="female" class="pull-right text-center col-md-5 btn btn-default" style="height: 36px;"> 
 
    <i class="fa fa-2x fa-female text-center" style="color: purple"> 
 
    <input type="radio" name="gender" value="female"> 
 
    </i> 
 
</label>

+1

它还对每次点击的工作。第一次单击将图标从0旋转到360.第二次单击图标并将其从360旋转到360. – zzzzBov

+0

@zzzzBov哦该死!你是对的!谢谢 – soheil

回答

1
maleRotation = 0; 
$(document).on("click", "#male", function() { 
     maleRotation+= 360; 
     $('#male i').css({ 
      'transform': 'rotate('+maleRotation+'deg)' 
     }) 
    }) 
相关问题