【问题标题】:Select entire contents of input and textarea on focus with Bootstrap/jQuery使用 Bootstrap/jQuery 选择 input 和 textarea 的全部内容
【发布时间】:2014-01-21 05:57:53
【问题描述】:

我在 Bootstrap 模式的表单中的输入和文本区域字段中都有文本。我想要这样当用户在文本中单击时选择整个字段值,因为这些是他们需要复制的预填充的 url 和 html 片段。

我读到需要超时,并使用jQuery focus on content of input / textarea - not working properly in IE8的方法

$('input[type=text]').live('focus', function() {
    var $th = $(this);
    setTimeout(function(){$th.select();}, 50);
});

但每次我打开模式时都会收到错误Uncaught TypeError: Object [object Object] has no method 'live'。我想知道是不是因为我只是在打开模式时设置我的输入值?

如果您转到http://culturepics.org/ 并单击任何“添加此”按钮,您可以看到它的实际效果。

【问题讨论】:

  • 我不确定您使用的是哪个版本的 jQuery,但 .live 已被弃用。您是否尝试过将您的代码与.on 一起使用? api.jquery.com/live

标签: jquery twitter-bootstrap input


【解决方案1】:

如果你的页面上有 Twitter Bootstrap,那么.live() 肯定不会工作。您正在使用已弃用的 jQuerys 函数。在这种情况下,.on 是您所需要的。

来自 jQuery

从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应该使用 .delegate() 而不是 .live()。

【讨论】:

  • 感谢您和@pedalpete - 就是这样(我在最新的 Bootstrap 上)。小学生错误,以及谷歌搜索旧代码的危险!干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 2016-02-25
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多