JavaScript array.toLocaleString() 函数
javascriptfront end technologyobject oriented programming
JavaScript array.toLocaleString() 函数将数组的元素作为字符串返回,并以特定于语言环境的字符串(如逗号)分隔。它可以将语言环境作为参数,该参数指定要将字符串转换为的语言标签。
以下是 array.toLocaleString() 函数的代码
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript array.toLocaleString()</h1> <div class="sample"></div> <button class="btn">点击此处</button> <h3>Click on the above button to convert the array to UK locale string</h3> <script> let fillEle = document.querySelector(".sample"); let arr = [1, 3, new Date()]; fillEle.innerHTML = arr; document.querySelector(".Btn").addEventListener("click", () => { arr = arr.toLocaleString("en-GB"); fillEle.innerHTML = arr; }); </script> </body> </html>
输出
点击“CLICK HERE”按钮 −