是否有可能获得用户的写作模式和写作方向



是否有可能获得用户的写作模式?我想知道文本是RTL还是LTR和TB。

看来我可以从计算值获得方向和写作模式。

#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模式?

正在获取方向和写作模式或语言以显示其他语言的考虑?

https://www.w3.org/tr/css-writing-modes-3/

https://developer.mozilla.org/en-us/docs/web/css/css_flexible_box_box_box_layout/relationship_of_flexbox_to_flexbox_to_other_other_layout_methods_methods#writing_modes_modes

答案:

使用getComputedStyle Window方法。

 getComputedStyle(document.body).direction;
 getComputedStyle(document.body)["writing-mode"];

为什么?

与默认情况下可能返回空字符串的节点的style对象不同(A.E. direction默认为" LTR",但会返回"(, getCompentedStyle 将返回每次都请求造型。


示例:

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);

HTML文档具有"方向"属性,该属性指定了其内容的语言方向,但这并不代表用户的阅读方向(即,美国的美国用户可以访问日本网站和内容的阅读方向是RTL(。

相反,您可以使用JavaScript获取用户的浏览器语言来检查全局属性:

navigator.language

这将为您提供ISO语言(即" EN-US"(代码语言。

相关内容

最新更新