How to detect text direction of element using Javascript?

前端 未结 4 786
渐次进展
渐次进展 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:00

    @explosion-pills answer is correct. I did some more research for IE compatibility and came up with the following:

    function getDirection(el) {
        var dir;
        if (el.currentStyle)
            dir = el.currentStyle['direction'];
        else if (window.getComputedStyle)
            dir = getComputedStyle(el, null).getPropertyValue('direction');
        return dir;
    }
    

    This should even work on Firefox 3.6 which requires null as the second parameter to getPropertyValue.

    Since this gives more information I thought I would post it in case it helps someone.

提交回复
热议问题