This question already has answers here:
In what JS engines, specifically, are toLowerCase & toUpperCase locale-sensitive?
(2个答案)
5年前关闭。
我试图用fiddle和toLocaleLowerCase()方法来toLowerCase()。
我的问题是:
什么是语言环境,因为这两个函数似乎都返回相似的输出? 这两种方法有什么区别? 为什么 fiddle 代码无法执行? 这些方法返回新字符串,并且不修改原始字符串(JavaScript字符串是不可变的)。您需要将值重新分配回元素。
工作片段:
(2个答案)
5年前关闭。
我试图用fiddle和toLocaleLowerCase()方法来toLowerCase()。
function ByLocale() {
document.getElementById("demo").innerText.toLocaleLowerCase();
}
function ByLower() {
document.getElementById("demo").innerText.toLowerCase();
}
<p>Click the button to convert the string "HELLO World!" to lowercase letters.</p>
<button onclick="ByLocale();">By Locale LowerCase</button>
<button onclick="ByLower();">By LowerCase</button>
<p id="demo">HELLO World!</p>
我的问题是:
最佳答案
与toLowerCase
不同,toLocaleLowerCase
将本地化考虑在内。在大多数情况下,对于大多数语言,它们将产生相似的输出,但是某些语言的行为会有所不同。
Check out the description on MDN:
为了完整起见,toUpperCase
和toLocaleUpperCase
的行为类似,除了使用大写字母外。
现在,您的代码片段什么都不做的问题。实际上有两个问题。
innerText
是非标准的,不适用于所有浏览器。请改用textContent
,仅添加innerText
以支持IE的旧版本。 工作片段:
function ByLocale() {
var el = document.getElementById("demo");
el.textContent = el.textContent.toLocaleLowerCase();
}
function ByLower() {
var el = document.getElementById("demo");
el.textContent = el.textContent.toLowerCase();
}
<p>Click the button to convert the string "HELLO World!" to lowercase letters.</p>
<button onclick="ByLocale();">By Locale LowerCase</button>
<button onclick="ByLower();">By LowerCase</button>
<p id="demo">HELLO World!</p>