標識不工作性質要選擇上重點從輸入的所有文本:jQuery的。選擇()上的焦點事件
<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
一次。你會看到,每次嘗試都會選擇文本。我不知道爲什麼。
P.S. 當您更改focus
到click
它完美的作品:
$('#selectMe').click(function() {
$(this).select();
console.log('focus fired');
});
,所以我想有一些問題focus
事件。
[抄襲](http://stackoverflow.com/questions/480735/select-all-contents-of-textbox-when-it簡化代碼-receives對焦的JavaScript或 - jQuery的)! – billyonecan
@billyonecan所以你說的是因爲別人寫的,這意味着我不能得出相同的結論嗎? –
不,更多的是因爲它是相同的字 – billyonecan