toLocaleLowerCase()和toLowerCase()之间的区别 [英] Difference between toLocaleLowerCase() and toLowerCase()

查看:136
本文介绍了toLocaleLowerCase()和toLowerCase()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用小提琴。 com / jsref / jsref_tolocalelowercase.asprel =noreferrer> toLocaleLowerCase()和 toLowerCase ()方法。



  function ByLocale(){document。 getElementById(demo)。innerText.toLocaleLowerCase();} function ByLower(){document.getElementById(demo)。innerText.toLowerCase();}  

< pre class =snippet-code-html lang-html prettyprint-override> < p>点击按钮转换字符串HELLO World!小写字母。< / p>< button onclick =ByLocale();>按Locale LowerCase< / button>< button onclick =ByLower();> By LowerCase< / button>< p id =demo> HELLO World!< / p>



我的问题是:




  • 什么是区域设置,因为这两个函数似乎都返回类似的输出?

  • 这两种方法有什么区别?

  • 为什么小提琴代码没有被执行?


解决方案

toLowerCase 不同, toLocaleLowerCase 考虑到本地化。在大多数情况下,对于大多数语言,它们会产生类似的输出,但某些语言的行为会有所不同。



查看MDN上的说明:


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


为了完整性, toUpperCase toLocaleUpperCase 表现得很好同样,除了上壳外。






现在你的代码片段没有做任何事情。实际上有2个问题。


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


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




工作片段:



  function ByLocale(){var el =的document.getElementById( 演示); el.textContent = el.textContent.toLocaleLowerCase();} function ByLower(){var el = document.getElementById(demo); el.textContent = el.textContent.toLowerCase();}  

 < p>点击按钮转换字符串HELLO World!小写字母。< / p>< button onclick =ByLocale();>按Locale LowerCase< / button>< button onclick =ByLower();> By LowerCase< / button>< p id =demo> HELLO World!< / p>  


I was trying to fiddle with toLocaleLowerCase() and toLowerCase() methods.

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?

解决方案

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

Check out the description on 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.

For completeness, toUpperCase and toLocaleUpperCase behave likewise, except with upper-casing.


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

  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. innerText is non-standard, and will not work across all browsers. Use textContent instead, and only add innerText to support old versions of 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>

这篇关于toLocaleLowerCase()和toLowerCase()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆