Jquery .select()在焦点事件上不起作用[重复]

Posted

技术标签:

【中文标题】Jquery .select()在焦点事件上不起作用[重复]【英文标题】:Jquery .select() doesnt work property on focus event [duplicate] 【发布时间】:2013-06-28 14:31:58 【问题描述】:

我想在聚焦时从输入中选择所有文本:

<input id="selectMe" value="123" />
<input type="button" id="focusHim" value="Im working fine" />

JS:

$('#selectMe').focus( function() 
    $(this).select();
    console.log('focus fired');
);

// little test if focus event works fine
$('#focusHim').click( function() 
    $('#selectMe').focus();
);

这个简化的代码可以正常工作,但有时会出现一些问题。

这里有小提琴:http://jsfiddle.net/daxv2/

要查看我的问题,请点击输入#selectMe,然后点击其他地方并再次点击#selectMe。您会看到,每次尝试都会选择文本。我不知道为什么。

附: 当您将 focus 更改为 click 时,它可以完美运行:

$('#selectMe').click( function() 
        $(this).select();
        console.log('focus fired');
    );

所以我想focus 事件有问题。

【问题讨论】:

【参考方案1】:

WebKit 浏览器似乎因为 mouseup 事件而干扰了这一点。

我添加了这个,它似乎工作正常:

$('#selectMe').mouseup(function () 
    return false;
);

http://jsfiddle.net/daxv2/6/

【讨论】:

Plagiarism! @billyonecan 所以你是说因为别人写的,这意味着我不能得出同样的结论? 不,更多的是因为它一字不差 @billyonecan umm .. 我检查过但没有。让你的事实直截了当。【参考方案2】:

您可以使用click 事件而不是focus。它工作正常,您也可以使用this.select() 而不是$(this).select() 来简化您的代码 http://jsfiddle.net/daxv2/8/

【讨论】:

我知道click 活动,但感谢this.select()

以上是关于Jquery .select()在焦点事件上不起作用[重复]的主要内容,如果未能解决你的问题,请参考以下文章

我的Android进阶之旅------&gt;Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法

jquery click 在 ipad/iphone 上不起作用

点击事件在 iPhone 4s 上不起作用

jQuery input 下拉框焦点事件

jquery js 当文本框获得焦点时,自动选中里面的文字

jQuery之jQuery扩展和事件