我试图fiddle http://jsfiddle.net/xameeramir/kr33b0aL/ with toLocaleLowerCase() http://www.w3schools.com/jsref/jsref_tolocalelowercase.asp and toLowerCase() http://www.w3schools.com/jsref/jsref_tolowercase.asp方法。
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>
我的问题是:
- What is Locale,因为这两个函数似乎返回相似的输出?
- 这两种方法有什么区别?
- 为什么小提琴代码没有被执行?
Unlike toLowerCase
, toLocaleLowerCase
考虑到本地化。在大多数情况下,对于大多数语言,它们会产生类似的输出,但某些语言的行为会有所不同。
看看MDN上的描述: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase
The toLocaleLowerCase()
方法返回根据任何特定于区域设置的大小写映射转换为小写的字符串值。toLocaleLowerCase()
不影响字符串本身的值。在大多数情况下,这会产生与以下相同的结果toLowerCase()
,但对于某些语言环境(例如土耳其语),其大小写映射不遵循 Unicode 中的默认大小写映射,可能会出现不同的结果。
为了完整性,toUpperCase
and toLocaleUpperCase
行为类似,但大写除外。
现在,针对您的代码片段没有执行任何操作的问题。实际上有2个问题。
-
这些方法返回新字符串并且不会修改原始字符串(JavaScript 字符串是不可变的)。您需要将值重新分配回元素。
-
innerText
is non-standard, and will not work across all browsers. Use textContent
instead, and only add innerText
to support old versions of IE. UPDATE: innerText https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText is now standardized, though it was not at the time this answer was posted.
工作片段:
const demo = document.getElementById('demo');
const str = demo.textContent;
function toLowerCase() {
demo.textContent = str.toLowerCase();
}
function toLocaleLowerCase() {
demo.textContent = str.toLocaleLowerCase();
}
function toLocaleLowerCaseDe() {
demo.textContent = str.toLocaleLowerCase('de-DE');
}
function toUpperCase() {
demo.textContent = str.toUpperCase();
}
function toLocaleUpperCase() {
demo.textContent = str.toLocaleUpperCase();
}
function toLocaleUpperCaseDe() {
demo.textContent = str.toLocaleUpperCase('de-DE');
}
function reset() {
demo.textContent = str;
}
<p>Click the buttons to apply each function to the string below.</p>
<button onclick="toLowerCase();">toLowerCase</button>
<button onclick="toLocaleLowerCase();">toLocaleLowerCase</button>
<button onclick="toLocaleLowerCaseDe();">toLocaleLowerCase("de-DE")</button>
<br><br>
<button onclick="toUpperCase();">toUpperCase</button>
<button onclick="toLocaleUpperCase();">toLocaleUpperCase</button>
<button onclick="toLocaleUpperCaseDe();">toLocaleUpperCase("de-DE")</button>
<br><br>
<button onclick="reset();">reset</button>
<p id="demo">WELCOME TO THE WORLD FÊTE! In HAUPTSTRAßE, İstanbul.</p>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)