admin管理员组

文章数量:1022525

How does one in iOS (iPad/iPhone/iPod) to force the focus on an input element when the DOM is ready, with jQuery?

I've got a barcode webpage, that needs the focus to be immediately on a textfield.

I've found jQuery will do just fine for IE,Firefox,Chrome,Safari,Android but iOS mobile Safari, and iOS mobile Chrome won't let me focus on a textfield when the DOM is ready.

This is what I have so far:

$j(document).ready(function() {
    $j('#barcode').focus();
});

Any ideas, guys/gals?

How does one in iOS (iPad/iPhone/iPod) to force the focus on an input element when the DOM is ready, with jQuery?

I've got a barcode webpage, that needs the focus to be immediately on a textfield.

I've found jQuery will do just fine for IE,Firefox,Chrome,Safari,Android but iOS mobile Safari, and iOS mobile Chrome won't let me focus on a textfield when the DOM is ready.

This is what I have so far:

$j(document).ready(function() {
    $j('#barcode').focus();
});

Any ideas, guys/gals?

Share Improve this question edited Aug 30, 2013 at 5:15 rink.attendant.6 46.5k64 gold badges110 silver badges157 bronze badges asked Aug 29, 2013 at 19:46 user2730049user2730049 111 silver badge3 bronze badges 1
  • Nothing to show - all the code is thus: $j(document).ready(function() { $j('#barcode').focus(); }); – user2730049 Commented Aug 29, 2013 at 20:44
Add a ment  | 

1 Answer 1

Reset to default 4

It is not possible on Mobile Safari and presumably Chrome on iOS. See the following duplicate SO questions:

  • How do I focus an HTML text field on an iPhone (causing the keyboard to e up)?

  • Mobile Safari: Javascript focus() method on inputfield only works with click

Running this fiddle in Mobile Safari or Chrome on iOS will confirm this answer.

$("input").focus(); // Does not focus input in Mobile Safari or Chrome on iOS.

This code has been tested (and failed) in Mobile Safari and Chrome on iOS on the following devices:

  • iPhone 4 running iOS 7 beta 6
  • iPad 2 running iOS 6.1.3
  • iOS Simulator running iOS 7 beta 5

How does one in iOS (iPad/iPhone/iPod) to force the focus on an input element when the DOM is ready, with jQuery?

I've got a barcode webpage, that needs the focus to be immediately on a textfield.

I've found jQuery will do just fine for IE,Firefox,Chrome,Safari,Android but iOS mobile Safari, and iOS mobile Chrome won't let me focus on a textfield when the DOM is ready.

This is what I have so far:

$j(document).ready(function() {
    $j('#barcode').focus();
});

Any ideas, guys/gals?

How does one in iOS (iPad/iPhone/iPod) to force the focus on an input element when the DOM is ready, with jQuery?

I've got a barcode webpage, that needs the focus to be immediately on a textfield.

I've found jQuery will do just fine for IE,Firefox,Chrome,Safari,Android but iOS mobile Safari, and iOS mobile Chrome won't let me focus on a textfield when the DOM is ready.

This is what I have so far:

$j(document).ready(function() {
    $j('#barcode').focus();
});

Any ideas, guys/gals?

Share Improve this question edited Aug 30, 2013 at 5:15 rink.attendant.6 46.5k64 gold badges110 silver badges157 bronze badges asked Aug 29, 2013 at 19:46 user2730049user2730049 111 silver badge3 bronze badges 1
  • Nothing to show - all the code is thus: $j(document).ready(function() { $j('#barcode').focus(); }); – user2730049 Commented Aug 29, 2013 at 20:44
Add a ment  | 

1 Answer 1

Reset to default 4

It is not possible on Mobile Safari and presumably Chrome on iOS. See the following duplicate SO questions:

  • How do I focus an HTML text field on an iPhone (causing the keyboard to e up)?

  • Mobile Safari: Javascript focus() method on inputfield only works with click

Running this fiddle in Mobile Safari or Chrome on iOS will confirm this answer.

$("input").focus(); // Does not focus input in Mobile Safari or Chrome on iOS.

This code has been tested (and failed) in Mobile Safari and Chrome on iOS on the following devices:

  • iPhone 4 running iOS 7 beta 6
  • iPad 2 running iOS 6.1.3
  • iOS Simulator running iOS 7 beta 5

本文标签: javascriptHow do I force focus on a texfield when the DOM is ready in iOSStack Overflow