【问题标题】:jQuery .focus() on textarea not working文本区域上的 jQuery .focus() 不起作用
【发布时间】:2013-03-27 17:53:29
【问题描述】:
我得到了这个代码:
<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>
还有这个:
$('#status').focus(function() {
alert('focused!');
});
我希望在 textarea 聚焦时启动警报,但它不起作用...
【问题讨论】:
标签:
jquery
focus
textarea
【解决方案1】:
Here's the working fiddle.
HTML
<textarea id="status" placeholder="Write here..." name="new_entry"></textarea>
JS
$('document').ready(function(){
$('#status').focus(function() {
alert('focused!');
});
});
【解决方案2】:
如果您尝试绑定焦点的文本区域是动态加载的[使用 ajax 或其他东西],那么您必须绑定焦点的实时事件。像这样的
$('#status').live('focus', function() {
alert('focused!');
});
【解决方案3】:
试试这个:
$(function(){
$('#status').focus(function() {
alert('focused!');
});
});
【解决方案4】:
您的 jquery 编写正确。看到这个类似的fiddle。使用调试器(在 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");
});
【解决方案5】:
试试这个:
$(function(){
$('#status').click(function() {
alert('focused!');
});
});
希望对你有帮助