首页 > 解决方案 > javascript 创建输入时选择 iOS 键盘

问题描述

通过 javascript 创建输入时,移动 safari 不会选择正确的键盘。first input显示小数点键盘,但second input仅显示数字和标点键盘

我怎样才能解决这个问题?

let myInput = document.createElement('input');
myInput.id = 'second';
myInput.type = 'number';
myInput.min = '0';
myInput.step = 'any';
myInput.inputmode = 'decimal';

document.getElementById('here').appendChild(myInput);
first input: <input id='first' type='number' min='0' step='any' inputmode='decimal'>

<div id='here'>second input: </div>

标签: javascriptiosinputkeyboard

解决方案


而不是使用:

myInput.inputmode = 'decimal';

用这个:

myInput.setAttribute('inputmode','decimal');

或者

myInput.inputMode = 'decimal'; // uppercase M

说明:一般来说,如果你使用javascript ,html 节点的“DOM 属性”通常是用驼峰命名的!在HTML中,您将使用“alllowercase”版本。“不要问为什么。;-)”。但更推荐在节点上使用方法,因为无论你写什么,这总是以 DOM 元素属性结束setAttribute()


推荐阅读