gpt4 book ai didi

javascript - 为什么 toLowerCase() 反转文本——为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:35 25 4
gpt4 key购买 nike

我正在使用 toLowerCase() 将内容可编辑的 div 转换为小写。但它也在颠倒文字!谁能告诉我为什么会这样?

$(document).ready(function() {
$('#user').bind('keyup', function() {
$(this).text($(this).text().toLowerCase());
});
});
#user {
background: #f1f1f1;
padding: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="user" contenteditable="true"></div>

fiddle : https://jsfiddle.net/professorsamoff/8zyvc202/3/

谢谢,蒂姆

最佳答案

这与浏览器的怪癖有关......当设置文本时,光标移动到可编辑区域的开头。

一个不太古怪的解决方案可能是在用户键入时使用 CSS 将其转换为小写。然后,如果您想超越 self ,请在 blur 事件中使用 JavaScript 进行实际转换。

$(document).ready(function() {
$('#user').blur(function() {
console.log('Converting from '+$(this).text()+' to '+$(this).text().toLowerCase());
$(this).text($(this).text().toLowerCase());
});
});
#user {
background: #f1f1f1;
padding: 1em;
text-transform:lowercase;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="user" contenteditable="true"></div>

关于javascript - 为什么 toLowerCase() 反转文本——为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com