是否有可能获得用户的写作模式?我想知道文本是RTL还是LTR和TB。
看起来我可以从computedValue获得方向和写作模式。
#div2 {
writing-mode: vertical-lr;
}
#div1 {
direction: rtl;
}
p {
background-color: #FFEECC;
}<p>While the characters in most scripts are written from left to right, certain scripts are written from right to left.</p>
<p id="div1">While the characters in most scripts are written from left to right, certain scripts are written from right to left. In some documents, in particular those written with the Arabic or Hebrew script, and in some mixed-language contexts, text in a single (visually displayed) block may appear with mixed directionality. This phenomenon is called bidirectionality, or "bidi" for short.</p>
<p id="div2">While the characters in most scripts are written from left to right, certain scripts are written from right to left. I</p>
或者是否有一种获得这些信息的推荐方法,比如IME模式?
获得方向和写作方式或语言是表示对其他语言的考虑吗?
发布于 2019-07-08 21:59:40
答案:
使用getComputedStyle Window方法。
getComputedStyle(document.body).direction;
getComputedStyle(document.body)["writing-mode"];为什么?
与简单地访问节点的style对象不同,节点对象默认可能返回空字符串(a.e )。direction默认为"ltr“,但将返回”),getComputedStyle将返回每次请求的样式值(包括默认值)。
示例:
let direction = getComputedStyle(document.body).direction;
let writing_mode = getComputedStyle(document.body)["writing-mode"];
console.log(direction, writing_mode);
撇开:
它也可以与解构分配结合使用。
let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);
let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);
console.log(direction, writing_mode);
发布于 2019-07-08 22:06:38
HTML文档有一个指定内容语言方向的“方向”属性,但这并不代表用户的阅读方向(即美国的美国用户可以访问日本网站,内容的阅读方向是RTL)。
相反,您可以使用Javascript获取用户的浏览器语言来检查全局属性:
navigator.language这将为您提供一个ISO语言(即"en-US")代码,您可以将其与RTL语言的ISO代码数组(易于从google收集)以及BTT语言的ISO代码列表进行比较。
https://stackoverflow.com/questions/56942964
复制相似问题