Javascript onclick需要两次单击 [英] Javascript onclick requiring two clicks

查看:88
本文介绍了Javascript onclick需要两次单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个方框,单击时会变成一个更大的方框(获取类). 但这需要2次点击,而不仅仅是点击.

I have this box that transforms in a bigger box when clicked (getting class). But it is taking 2 clicks, and not only one as it was suposed to take.

.clientes {width:170px;height:27px;background-image:url('../imagens/clients.gif');-webkit-transition:1s;}
.clientes-clicked {width:356px !important;height:154px !important;background-image:url('../imagens/clients-big.png') !important;-webkit-transition:1s;}

<script>    
    var clientesclick = function(){
    $('.clientes').on('click', function(e) {
      $('.clientes').toggleClass("clientes-clicked"); //you can list several class names 
      e.preventDefault();
    });
    }
</script>

推荐答案

您正在使它变得比所需的更加复杂.您可以简单地做到:

You're making this more complicated than it needs to be. You could simply do:

$('.clientes').click(function(){
    $(this).toggleClass('clientes-clicked');
});

小提琴: http://jsfiddle.net/64XQ3/

而且,如上所述,您的jQuery应该包装在

And, as was pointed out above, your jQuery should be wrapped in

$(document).ready(function(){
    // code here
});

这篇关于Javascript onclick需要两次单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆