jquery 小写
jQuery 小写转换方法
在 jQuery 中,可以通过 JavaScript 原生方法或 jQuery 提供的功能实现字符串的小写转换。
使用 JavaScript 原生方法
var str = "HELLO WORLD";
var lowerStr = str.toLowerCase();
console.log(lowerStr); // 输出: "hello world"
在 jQuery 选择器中使用小写转换
$("input[type='text']").val(function(index, value) {
return value.toLowerCase();
});
处理用户输入时转换为小写
$("#myInput").on("keyup", function() {
$(this).val($(this).val().toLowerCase());
});
批量转换元素文本为小写
$(".to-lowercase").each(function() {
$(this).text($(this).text().toLowerCase());
});
注意事项
toLowerCase()方法不会改变原始字符串,而是返回新字符串- 转换仅针对 ASCII 字符,某些 Unicode 字符可能需要特殊处理
- 在处理表单提交时,可以在提交前统一转换为小写
扩展方法
可以创建 jQuery 插件来扩展小写转换功能:
$.fn.toLowerCase = function() {
return this.each(function() {
if ($(this).is("input, textarea")) {
$(this).val($(this).val().toLowerCase());
} else {
$(this).text($(this).text().toLowerCase());
}
});
};
// 使用方式
$(".convert-me").toLowerCase();






