How to detect text direction of element using Javascript?

前端 未结 4 787
渐次进展
渐次进展 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 19:01

    You can simply use the style object:

    console.log(document.getElementById('baz').style.direction);
    

    DEMO

    Take note that this object of the DOM only represents the in-line styles of an element, it doesn't apply to any css style sheets.

提交回复
热议问题