在HTML页面中,我将文本框的样式设置为initialfortext-transform属性,但不使用initial。我还测试了其他值,比如uppercaselowercasecapitalize可以正常工作,但是没有变化。我希望当用户在文本框中输入文本时,文本应该以首字母大写,并在输入时保持不变。

<!DOCTYPE html>
<html>
<head>
<style>

::-webkit-input-placeholder {
   text-transform: initial;

}

:-moz-placeholder {
   text-transform: initial;
}

::-moz-placeholder {
   text-transform: initial;
}

:-ms-input-placeholder {
   text-transform: initial;
}
input{
   text-transform: initial;
}
</style>
</head>
<body>
<input type="text" placeholder="text" />
</body>
</html>

也试过了
<html>
<head>
<style>
input:first-letter {
    text-transform: uppercase;
}
</style>
</head>
<body>

<input type='text' >

</body>
</html>

运气不好。

最佳答案

您可以在函数中执行此操作:
例如:

$(document).ready(function() {
    $('input').on('keypress', function(event) {
        var $this = $(this),
        val = $this.val();
        val = val.charAt(0).toUpperCase() + val.substr(1);
        $this.val(val);
   });
});

这将为您提供所需的输出。希望这有帮助:)

10-01 04:43
查看更多