How to detect text direction of element using Javascript?

前端 未结 4 953
渐次进展
渐次进展 2021-02-06 18:19

What\'s the best way to detect the text direction of an html element using Javascript? I would expect to get either \"rtl\" or \"ltr\".

4条回答
  •  忘掉有多难
    2021-02-06 18:46

    getComputedStyle is available in modern browsers (IE9+ and the others).

    getComputedStyle(document.getElementById('foo')).direction
    

    http://jsfiddle.net/m8Zwk/

    Reference to getComputedStyle on Mozilla Developer Network

提交回复
热议问题