是否可以获取用户的书写模式?我想知道文本是 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 模式?
获取方向和写作模式或语言是表达对其他语言的考虑的方式吗?
https://www.w3.org/TR/css-writing-modes-3/
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Relationship_of_Flexbox_to_Other_Layout_Methods#Writing_Modes
回答:
使用 getComputedStyle
窗口
方法。
getComputedStyle(document.body).direction;
getComputedStyle(document.body)["writing-mode"];
为什么?
不同于简单地访问节点的 style
对象,默认情况下可能返回空字符串(即 direction
默认为“ltr”但将返回“”), getComputedStyle 每次都会返回请求的样式值,包括默认值。
示例:
let direction = getComputedStyle(document.body).direction;
let writing_mode = getComputedStyle(document.body)["writing-mode"];
console.log(direction, writing_mode);
旁白:
它也可以与Destructuring Assignment结合使用:
let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);
let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);
console.log(direction, writing_mode);
我是一名优秀的程序员,十分优秀!