4

当我尝试确定默认文本方向时,这将返回一个空字符串......

alert(document.getElementById('example').dir);

我想确定默认文本方向是 ltr 还是 rtl。

4

4 回答 4

4

ltr/rtl 是用 css 的“direction”属性定义的。它也可以使用 DOM 中元素的“dir”属性来定义。

如果要检查属性值,请使用 getAttribute 方法:

 document.getElementById('example').getAttribute('dir');

另外,检查元素的样式。它默认为 ltr,因此如果未定义,则应作为空字符串返回。否则它应该说“rtl”

 document.getElementById('example').style.direction;

https://developer.mozilla.org/en/CSS/direction

于 2012-06-08T00:46:17.907 回答
3

我会用

var elm = document.getElementById('example') || document.body;
return window.getComputedStyle(elm).direction;
于 2012-06-08T00:47:01.687 回答
0

如果它为空白,则表示它未在元素的dir属性中指定。

于 2012-06-08T00:45:26.737 回答
0

我在调试期间发出警报,如下所示:

alert(window.getComputedStyle(document.getElementById('editor_rich')).direction);
于 2012-06-09T08:48:28.997 回答