Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何只在textarea的焦点上执行一次代码?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何只在textarea的焦点上执行一次代码?

Javascript 如何只在textarea的焦点上执行一次代码?,javascript,jquery,html,Javascript,Jquery,Html,我有一个这样的茶壶: $textarea.on'focus',函数{ 警报点击; }; 您可以使用jQuery的 对于每个事件类型,每个元素最多执行一次处理程序 您可以使用jQuery的 对于每个事件类型,每个元素最多执行一次处理程序 您可以使用计数器系统,如果您改变主意要触发多少次聚焦警报,您只需更改if-then语句值即可 //initalize the counter var count = 0; //set the limit var limit = 0; //on focus e

我有一个这样的茶壶:

$textarea.on'focus',函数{ 警报点击; }; 您可以使用jQuery的

对于每个事件类型,每个元素最多执行一次处理程序

您可以使用jQuery的

对于每个事件类型,每个元素最多执行一次处理程序


您可以使用计数器系统,如果您改变主意要触发多少次聚焦警报,您只需更改if-then语句值即可

//initalize the counter
var count = 0;

//set the limit
var limit = 0;

//on focus event
$("textarea").on('focus', function(){

   //if the count equals the limit
   if(count === limit){

     //alert the message
     alert("clicked");

   }

//increment the count
count++;

});

您可以使用计数器系统,如果您改变主意要触发多少次聚焦警报,您只需更改if-then语句值即可

//initalize the counter
var count = 0;

//set the limit
var limit = 0;

//on focus event
$("textarea").on('focus', function(){

   //if the count equals the limit
   if(count === limit){

     //alert the message
     alert("clicked");

   }

//increment the count
count++;

});


您可以设置variable first_time=true并在中检查该变量focus@JasminMistry是的,我已经知道这个方法了。。但就我个人而言,我讨厌在这种用法中使用变量。。!是的,你是对的。adeneo的ans是完美的。您可以将变量first\u time=true设置为,并在中检查该变量focus@JasminMistry是的,我已经知道这个方法了。。但就我个人而言,我讨厌在这种用法中使用变量。。!是的,你是对的。adeneo的ans很完美这正是我想要的+1我可以再问一个简短的问题吗?我有两个事件,我想通过或来混合它们。这个:$document.on'click','textarea_tools>i',function{和这个$qandextarea.on'focus',function{{,这可能吗?不太确定你在问什么,但可能是这样的->这正是我要找的…+1我可以问另一个简短的问题吗?我有两个事件,我想通过或来混合它们。这个:$document.on'click','.textarea_tools>I',function{和这个$qanadextarea.on'focus',function{,这是可能的吗?不太确定你在问什么,但可能像这样->谢谢,它会起作用..但正如我在评论中提到的那样,我讨厌这样:-…+1为你的解决方案不是问题,我很抱歉,我在发布我的答案之前没有看到你的评论。谢谢,它会起作用..但正如我在评论中提到的,我讨厌这种方式:-…+1对于你的解决方案不是问题,我很抱歉,在我发布我的答案之前,我没有看到你的评论。