How to detect current web page or element direction (rtl/ltr) or any other attribute in javascript?

…衆ロ難τιáo~ 提交于 2021-02-10 05:12:48

问题


How can i read any attribute that currently effects an element that is not necessarily style? one such attribute would be "dir".


回答1:


Recently I had similar problem, you can get element's property with window.getComputedStyle and element.currentStyle methods:

    var elem = document.getElementById('test');
    if (window.getComputedStyle) { // all browsers
        cs = window.getComputedStyle(elem, null).getPropertyValue('direction');
    } else {
        cs = elem.currentStyle.direction; // IE5-8
    }
    alert(cs);

jsfiddle ; compatibility info




回答2:


These are generally reflected as properties on the element instance. dir is, for example, as are most others. Some have slightly renamed names (htmlFor instead of for, className instead of class), but for the most part it's 1:1 (target for target, action for action, ...).

References:

  • dir in the DOM2 HTML Spec
  • dir in the HTML5 Spec


来源:https://stackoverflow.com/questions/16957533/how-to-detect-current-web-page-or-element-direction-rtl-ltr-or-any-other-attri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!