dongni8124 2012-03-02 18:14
浏览 68
已采纳

需要双击的Jquery onmousedown?

I have searched before posting but none of the posts are that relevant (saying that i am new to Jquery).

My problem is when i click the "add friend" button it wont fire upon a single click but it needs a double click.

These are the relevant sections of code:

Setting the div:

$fField = <div style="display:inline; border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color#999; font-size:11px;">
  <a href="#" onclick="return false" onmousedown="javascript:friendArea(\'addFriend\');">Add Friend</a>
  </div>;

Javascript:

function friendArea(x){
    //alert(x);
    if($('#'+x).is(":hidden")){
        $('#'+x).slideDown(200);
    } else {
        $('#'+x).hide();
    }
    $('.friendSlide').hide();
}

the message to display (the one which needs 2 clicks to be displayed)

<div class="friendSlide" id="addFriend">Are you sure?</div>

Any help would be much appreciated! Oh and I'm using the latest version of jQuery.

  • 写回答

3条回答 默认 最新

  • dqm83011 2012-03-02 18:31
    关注

    Hmm, well, it looks like you are showing and hiding your div at the same time:

    HTML:

    <div class="friendSlide" id="addFriend">Are you sure?</div>
    

    JS:

    function friendArea(x){
        //alert(x);
        if($('#'+x).is(":hidden")){
            $('#'+x).slideDown(200);
        } else {
            $('#'+x).hide();
        }
        $('.friendSlide').hide();
    }
    

    You call friendArea('addFriend'), which is supposed to slideDown your div, but at the end of the function you hide .friendSlide, of which your div is also a class. You basically hide it every time. This can't be what you want.

    Comment out that line and it works: http://jsfiddle.net/Ggdhp/

    Of course, you might have had a different reason for that, but I think you are getting some divs and classes mixed up.

    Note: I would also suggest that you don't use inline css or javascript. It always makes debug much more difficult, and you miss typos as you end up having to put everything on a single line and escape quotes. For instance, color#999; in your div style is missing a colon.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入