如何捕获由 css 大写的大写输入字母:文本转换:大写
How to capture capitalized input letter which is capitalized by css: text-transform:capitalize
我发现用css大写的大写字母text-transform:capitalize
在被javascript捕获时没有大写。我想知道解决此问题的最简单方法是什么?
下面的演示:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<input id="box" type="text" style="text-transform:capitalize">
<button id="showinput">type in something then press me</button>
</html>
<script>
$("#showinput").click(function(){
txt=$("#box").val();
alert(txt+" as you can see, the first letter of each word is not capitalized!");
})
</script>
CSS 文本转换属性
仅转换用户在屏幕上看到的内容(与所有 CSS 属性一样)。它不会与你的 JavaScript 代码交互。我建议对 JavaScript 中的字符串应用类似的函数,例如 lodash 中的_.upperCase
。
正如 McMath 所说,CSS text-transform 不与 Javascript 交互。这是一个解决方案,可以通过在 Javascript 中大写第一个字母来给出您想要的结果:
$("#showinput").click(function(){
txt = $("#box").val();
txt = txt.charAt(0).toUpperCase() + txt.slice(1);
alert(txt+" as you can see, the first letter of each word is capitalized!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<input id="box" type="text" style="text-transform:capitalize">
<button id="showinput">type in something then press me</button>
</html>
来源:如何在 JavaScript 中使字符串的第一个字母大写?
使用正则表达式在 js 中执行此操作
$("#showinput").click(function(){
txt=$("#box").val();
txt = txt.trim().replace(/'b'w{3,}/g, function (l) {
return l.charAt(0).toUpperCase() + l.slice(1);
});
alert(txt+" as you can see, the first letter of each word is not capitalized!");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<input id="box" type="text" style="text-transform:capitalize">
<button id="showinput">type in something then press me</button>
</html>
测试用例:Hello World
注意 这也适用于输入的文本之间有空格的情况,样式text-transform:capitalize
处理每个单词大写的情况。
小提琴
脚本
$("#showinput").click(function(){
txt=$("#box").val();
var newtxt=txt.split(" ");
var tmp=[];
for(i=0;i<newtxt.length;i++)
{
tmp.push(newtxt[i].trim().charAt(0).toUpperCase()+ newtxt[i].slice(1));
}
//alert(tmp.join(" "));//YOU CAN USE THIS ALSO
alert(tmp.join().replace(","," ")+" as you can see, the first letter of each word is not capitalized!");//YOU CAN USE tmp.join(" ") also
})
描述
我尝试的是,首先获取数组中的每个单词,由space
分割。然后转换每个单词的第一个字母,同时修剪任何空格,然后将其与单词的其余部分连接起来,并将其全部放入数组中。
然后用 .join()
连接数组,然后用 space
替换,
。我希望这对你有帮助。
参见小提琴演示,它像你说的那样工作。
相关文章:
- 大写--文本区域中的文本选择
- 如何使用剑道富文本编辑器自动大写第一个字母
- 如何在phonegap中为Android自动大写文本输入
- 如何仅附加大写文本
- 如何使用 ExtendScript 在 InDesign 中以编程方式将文本设置为“小型大写字母”
- 如何捕获由 css 大写的大写输入字母:文本转换:大写
- 使用挂起的单选按钮将文本转换为小写/大写
- JavaScript - 带有大写数字的文本
- 内部HTML文本的大写问题
- 在文本框中输入文本时,如果字母表前面有空格,请将字母表输入大写
- 希腊语和文本转换:大写
- 将文本区域字段限制为大写字母、数字和&符号与jQuery
- 在JqxGrid内联编辑如何将文本更改为大写
- HTML输入文本:禁用全大写数字
- 检测文本区域中大写字母的使用情况
- 如何在使用acrobatjavascript的文本区域输入时大写每个字符
- jQuery/javascript的所有大写文本,无论id
- 用Javascript将输入文本大写
- 允许数字和文本(大写)
- .toUpperCase()按钮,用于将TextArea中的所有文本大写