jQuery专注于input / textarea的内容 - 在IE8中无法正常工作 [英] jQuery focus on content of input / textarea - not working properly in IE8

查看:107
本文介绍了jQuery专注于input / textarea的内容 - 在IE8中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望网站上的输入字段能够在用户第一次点击时选择所有文本,但在IE8中,文本会被选中一秒,然后恢复正常。



我的js代码:

  $(文档).ready(function(){// HTML DOM文档准备就绪
//将此行为添加到所有文本字段
$(input [type ='text'],textarea)。live (focus,function(){
//选择字段内容
this.select();
});
});

有什么想法?我已经尝试在.select()之后添加.focus(),并且奇怪的是它的工作原理,但是在FF和IE中抛出了大量的js错误。



谢谢

解决方案

在jQuery对象上尝试 .select() DOM元素,并将事件更改为单击

  $(input [type ='text']当您选择字段时,应该默认选择文本。 ).live(click,function(){
//选择字段内容
$(this).select();
});






好的,这个人在后面踢我因为某些原因。我可以发誓我之前做过这件事,但不知道怎么做。



这是我想到的最好的。它使用 setTimeout()推迟 .select(),直到click事件触发为止。也许有更好的方法?希望它有帮助。

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


I want the input fields on my site to select all the text when the user first clicks on them, but in IE8 the text is selected for a split second then reverts back to normal. Works fine in FF.

My js code:

$(document).ready(function () { //HTML DOM document is ready
    // Add this behavior to all text fields
    $("input[type='text'], textarea").live("focus", function(){
                                         // Select field contents
                                         this.select();
                                         });
});

Any ideas? I've tried adding ".focus()" after ".select()" and bizarrely enough it works, but throws loads of js errors in FF and IE.

Thanks

解决方案

Try .select() on a jQuery object instead of the DOM element, and change the event to click. When you tab into the field, the text should be selected by default.

$("input[type='text']").live("click", function(){
       // Select field contents
      $(this).select();
  });


Well, this one was kicking me in the rear for some reason. I could swear I've done this before, but couldn't figure it out how.

This is the best I came up with. It postpones the .select() using setTimeout() until after the click event fires. Maybe there's a better way? Hope it helps.

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

这篇关于jQuery专注于input / textarea的内容 - 在IE8中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆