简体   繁体   English

在textarea中聚焦模糊并使用jquery显示隐藏范围

[英]Focus Blur in textarea and show hide span using jquery

I have this textarea : 我有这个textarea:

<textarea  name="comment" id="comment" cols="75" rows="5" class="txtarea"></textarea>

and this span for show character limits: 以及显示字符限制的范围:

<span style="display:none" id="chars_left">1000</span>

now i need to show/hide span when focus/blur in textarea box. 现在我需要在textarea框中聚焦/模糊时显示/隐藏跨度。 i create this jquery function, but this not work for me. 我创建了这个jquery函数,但这对我不起作用。 what's problem ? 什么问题 ? how to work jquery for my need? 如何为我的需要工作的jQuery?

<script>
 $('textarea').focus(function(){

      jQuery(this).find('#chars_left').show();       
})


// this function will hide divs when you leave that textarea
$('textarea').blur(function(){

      jQuery(this).find('#chars_left').hide();       
})
</script>

There were few things which required correction which you will see in the following text. 很少有事情需要更正,您将在下文中看到。 You can directly access control when you have id, also Provide type in script tag. 拥有ID时,您可以直接访问控件,也可以在script标签中提供类型。 You were trying to find chars_left in the childs of textarea which was not required. 您试图在textarea的子项中找到chars_left,但这不是必需的。 You can use focusout event with focus. 您可以将焦点事件与焦点一起使用。

Live Demo 现场演示

<script type="text/javascript">
 $('textarea').focus(function(){    
      jQuery('#chars_left').show();       
})


// this function will hide divs when you leave that textarea
$('textarea').focusout(function(){

      jQuery('#chars_left').hide();       
})
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM