【问题标题】:JavaScript dir method returns empty string for text directionJavaScript dir 方法返回文本方向的空字符串
【发布时间】:2012-06-08 00:37:23
【问题描述】:

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

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

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

【问题讨论】:

  • 查看stackoverflow.com/questions/1169967/… 以访问继承的样式。
  • 不要编辑我的帖子,如果你不同意先评论;所有的修改都只是淡化了我的问题的细节和意义。

标签: javascript text bidirectional


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我会用

    var elm = document.getElementById('example') || document.body;
    return window.getComputedStyle(elm).direction;
    

    【讨论】:

    • 使用 alert(window.getComputedStyle(elm).direction);,谢谢。
    【解决方案3】:

    如果它是空白的,则意味着它没有在元素的dir 属性中指定。

    【讨论】:

      【解决方案4】:

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

      alert(window.getComputedStyle(document.getElementById('editor_rich')).direction);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        相关资源
        最近更新 更多