单击Focused DIV上的操作 [英] Click action on Focused DIV

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

问题描述

我有一个div,其tabindex属性设置为'-1',因此它可以专注于鼠标点击。

I have a div with tabindex attribute set to '-1' so it can gain focus on mouse click.

<div tabindex='-1'></div>

.div:focus{//some style}

我想要用户单击div时使用Jquery执行某些操作,但仅在选中时(第二次单击后)。

I want to do something with Jquery when the user clicks the div but only if it is selected (after second click).

$('div').click(function(){
if($(this).is(':focus')){
//do something
}
});

但它不起作用,立即触发行动。

But it doesn't work, the action is immediately triggered.

编辑:包含代码。

推荐答案

我会用标志检查第二次点击,看看我的 回答 这里

I would check the 2nd click with a flag, see my answer here


  1. 我使用了一个标记isClicked来确定它是第一次点击还是第二次点击。如果是第一次单击,将标志设置为true并返回((即)不执行任何操作)。下一次,标志被检查为真,这意味着它是第二次点击,下面的添加代码做你想要的。

  1. I used a flag isClicked to determine if it the 1st click or 2nd. If it is 1st click, set the flag to true and return ((i.e) do nothing). Next time, the flag is checked for true which means it is 2nd click, and the add code below to do what you want.

var isClicked = false;

 $('div#myTest').click(function(){

 //do nothing on 1st click
 if (isClicked == false) {
    alert('Clicked 1st time');     
    isClicked = true;
    return;
 }

 //do whatever u want on second click    
 alert('Clicked 2nd time');     

 });


  • 在div外单击时重置标志,下面的代码就是这样做的.. 。

  • Reset the flag when it is clicked outside the div, the below code does that...

    // to check it is clicked outside div
     $(document).click(function(e){
      if($(e.target).is('#myTest'))return;    
    
      //reset to
      isClicked = false;
    
     });
    


  • 这篇关于单击Focused DIV上的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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