首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有可能得到用户的写作方式和写作方向吗?

有可能得到用户的写作方式和写作方向吗?
EN

Stack Overflow用户
提问于 2019-07-08 21:51:11
回答 2查看 906关注 0票数 1

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

看起来我可以从computedValue获得方向和写作模式。

代码语言:javascript
复制
#div2 {
  writing-mode: vertical-lr;
}

#div1 {
  direction: rtl;
}

p {
  background-color: #FFEECC;
}
代码语言:javascript
复制
<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/

模式

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-08 21:59:40

答案:

使用getComputedStyle Window方法。

代码语言:javascript
复制
 getComputedStyle(document.body).direction;
 getComputedStyle(document.body)["writing-mode"];

为什么?

与简单地访问节点的style对象不同,节点对象默认可能返回空字符串(a.e )。direction默认为"ltr“,但将返回”),getComputedStyle将返回每次请求的样式值(包括默认值)。

示例:

代码语言:javascript
复制
let direction = getComputedStyle(document.body).direction;
let writing_mode = getComputedStyle(document.body)["writing-mode"];
console.log(direction, writing_mode);

撇开:

它也可以与解构分配结合使用。

代码语言:javascript
复制
let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);

代码语言:javascript
复制
let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);
console.log(direction, writing_mode);

票数 5
EN

Stack Overflow用户

发布于 2019-07-08 22:06:38

HTML文档有一个指定内容语言方向的“方向”属性,但这并不代表用户的阅读方向(即美国的美国用户可以访问日本网站,内容的阅读方向是RTL)。

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

代码语言:javascript
复制
navigator.language

这将为您提供一个ISO语言(即"en-US")代码,您可以将其与RTL语言的ISO代码数组(易于从google收集)以及BTT语言的ISO代码列表进行比较。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56942964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档