可以在 mobile safari 中使用 javascript 关闭自动大写吗?
-
16-09-2019 - |
题
Mobile safari 支持输入元素上的一个属性,称为 autocapitalize
[记录在这里],当设置为“关闭”时,iPhone 将停止将该字段中的文本输入大写,这对于 url 或电子邮件字段非常有用。
<input type="text" class="email" autocapitalize="off" />
但此属性在 html 5(或据我所知的另一个规范)中无效,因此将其包含在 html 中将产生无效的 html 页面,我想做的是能够将此属性添加到 onload 的特定字段中javascript 与这样的东西:
$(document).ready(function(){
jQuery('input.email, input.url').attr('autocapitalize', 'off');
});
它在 Firefox 和桌面 Safari 中添加了正确的属性,但在移动 Safari 中似乎没有执行任何操作,为什么?
解决方案
这应该在iPhone OS 3.0中修复。您正在尝试哪种版本的iPhone操作系统?
Email: <input id="email" type="text"><br>
URL: <input id="url" type="text"><br>
<script>
//document.getElementById("email").autocapitalize = 'off';
//document.getElementById("url").autocapitalize = 'on';
document.getElementById("email").setAttribute('autocapitalize', 'off');
document.getElementById("url").setAttribute('autocapitalize', 'on');
alert(document.body.innerHTML);
</script>
其他提示
边注。您可以通过将输入类型指定为“电子邮件”来自动调出“电子邮件”键盘(用于键入电子邮件的字符稍好一些),从而进一步改善 iOS 上的用户体验。
<input type="email" class="email" autocapitalize="off" />
这是一些文档 关于输入类型如何控制 iOS 键盘。
如果您通过脚本添加或在标记中添加它,则同样无效。只是验证器通过脚本添加,验证器将无法注意到它。
只需将其放入标记中,然后在其旁边发表评论,就像 <!-- the "autocapitalize" attribute is an Apple proprietary extension for the iPhone to change its IME behaviour -->
, ,这样看验证者中的代码的人就会知道发生了什么。
如果这是一个有用的功能,则只需在严格的验证和用户体验之间选择即可。就个人而言,我每天都会选择UX。
因此,正如Ddkilzer所建议的工作,我无法让JQuery做到这一点,但是简单的旧JavaScript,因此我将此功能放在一起,以将AutoCapitalize ='OFF'选项应用于具有特定类别的所有输入:
$(document).ready(function(){
// disable autocapitalize on .url, .email fields
unautocapitalize('url');
unautocapitalize('email');
});
function unautocapitalize(cssClass){
var elems = document.getElementsByClassName(cssClass);
for (var j = 0; j < elems.length; j++){
elems[j].setAttribute('autocapitalize', 'off');
}
}