Javascript toLocaleLowerCase() 和 toLowerCase() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34283414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:07:09  来源:igfitidea点击:

Difference between toLocaleLowerCase() and toLowerCase()

javascripthtmlstringlocalizationlocale

提问by xameeramir

I was trying to fiddlewith toLocaleLowerCase()and toLowerCase()methods.

我试图拨弄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>

My questions are:

我的问题是:

  • What is Locale, because both the functions seems to return similar output?
  • What is the difference between these two methods?
  • Why is the fiddle code not getting executed?
  • 什么是Locale,因为这两个函数似乎都返回相似的输出?
  • 这两种方法有什么区别?
  • 为什么小提琴代码没有被执行?

采纳答案by Alexander O'Mara

Unlike toLowerCase, toLocaleLowerCasetakes localization into account. In most cases, with most languages, they will produce similar output, however certain languages will behave differently.

与 不同toLowerCasetoLocaleLowerCase将本地化考虑在内。在大多数情况下,对于大多数语言,它们会产生相似的输出,但是某些语言的行为会有所不同。

Check out the description on MDN:

查看 MDN 上的描述:

The toLocaleLowerCase() method returns the value of the string converted to lower case according to any locale-specific case mappings. toLocaleLowerCase() does not affect the value of the string itself. In most cases, this will produce the same result as toLowerCase(), but for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.

toLocaleLowerCase() 方法根据任何特定于语言环境的大小写映射返回转换为小写的字符串的值。toLocaleLowerCase() 不会影响字符串本身的值。在大多数情况下,这将产生与 toLowerCase() 相同的结果,但对于某些区域设置,例如土耳其语,其大小写映射不遵循 Unicode 中的默认大小写映射,可能会有不同的结果。

For completeness, toUpperCaseand toLocaleUpperCasebehave likewise, except with upper-casing.

为了完整性,toUpperCasetoLocaleUpperCase表现同样,除了与上套管。



Now for the issue with your snippet not doing anything. There are actually 2 issues.

现在,您的代码段没有做任何事情的问题。其实有2个问题。

  1. These methods return new strings and don't modify the original (JavaScript strings are immutable). You will need to re-assign the value back to the element.

  2. innerTextis non-standard, and will not work across all browsers. Use textContentinstead, and only add innerTextto support old versions of IE.

  1. 这些方法返回新字符串并且不会修改原始字符串(JavaScript 字符串是不可变的)。您需要将值重新分配回元素。

  2. innerText是非标准的,并且不适用于所有浏览器。textContent改为使用,并且只添加innerText支持旧版本的 IE。

Working Snippet:

工作片段:

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>