Show keyboard for div with contenteditable=“true”

2019-04-14 06:17发布

问题:

I have a div with contenteditable="true". How can I set focus on this div via javascript(if it possible on pure js, without jquery)?

回答1:

Prior to iOS 6 this wasn't possible, but thankfully they've changed that. You can't do it in general for a website, but if you are embedding a UIWebView in your application you can do it now:

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.keyboardDisplayRequiresUserAction = NO;

Then your element.focus() will work and bring up the keyboard as desired.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIWebView/keyboardDisplayRequiresUserAction

More info on what changed with iOS 6 & WebKit:

http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166-CH1-SW19



回答2:

supposing the id of the contenteditable div is 'editableDiv',

document.getElementById('editableDiv').focus();

**Updates Could you try with something inside the div?

<script>
function showKeyboard() {
    document.getElementById('content').focus();
}
</script>
<body onLoad="showKeyboard()">
    <div id="content" contenteditable="true" height="1000">&nbsp;</div>  
</body>


回答3:

It is impossible in iOS now :( https://github.com/jquery/jquery-mobile/issues/3016