jQuery .focus() 在 textarea 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15665932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .focus() on textarea not working
提问by Kevin Lloyd Bernal
I got this code:
我得到了这个代码:
<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>
and this:
和这个:
$('#status').focus(function() {
alert('focused!');
});
I want the alert to start when the textarea is focused on, but it doesn't work...
我希望在 textarea 聚焦时启动警报,但它不起作用......
回答by abhshkdz
HTML
HTML
<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>
JS
JS
$('document').ready(function(){
$('#status').focus(function() {
alert('focused!');
});
});
回答by Ravi Sankar Rao
If the text area which your are trying to bind focus to is loaded dynamically [using ajax or something], then you have to bind the live event of focus. Something like this
如果您尝试将焦点绑定到的文本区域是动态加载的 [使用 ajax 或其他东西],那么您必须绑定焦点的实时事件。像这样的东西
$('#status').live('focus', function() {
alert('focused!');
});
});
回答by pala?н
Try this:
尝试这个:
$(function(){
$('#status').focus(function() {
alert('focused!');
});
});
回答by Bizmarck
Your jquery is written correctly. See this similar fiddle. Check with a debugger (in Chrome or Firebug), is the focus
method being called? You can try putting the code in a $('document').ready()
function.
您的 jquery 编写正确。看到这个类似的小提琴。检查调试器(在 Chrome 或 Firebug 中),是否focus
正在调用该方法?您可以尝试将代码放入$('document').ready()
函数中。
<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>
<textarea id="status2" placeholder="Write here..." name="new_entry"></textarea>
$('#status').focus(function() {
$('#status2').val("wheee");
});
回答by Jamie de Fretesz
try this instead :
试试这个:
$(function(){
$('#status').click(function() {
alert('focused!');
});
});
hope it'll help
希望它会有所帮助