How can I automatically set text direction based on input text language?

限于喜欢 提交于 2019-12-05 04:05:41
  1. list the right to left languages typical characters
  2. detect them on the fly (usual js events)
  3. change css classes accordingly

Or follow this link:

http://www.i18nguy.com/temp/rtl.html

I wrote a JQuery code to do this. Just add class "checkRTL" to the element you want to set its direction.

var rtlChar = /[\u0590-\u083F]|[\u08A0-\u08FF]|[\uFB1D-\uFDFF]|[\uFE70-\uFEFF]/mg;
$(document).ready(function(){
    $('.checkRTL').keyup(function(){
        var isRTL = this.value.match(rtlChar);
        if(isRTL !== null) {
            this.style.direction = 'rtl';
         }
         else {
            this.style.direction = 'ltr';
         }
    });
});

There's also Twitter's library, which may help:

https://github.com/twitter/RTLtextarea

Add dir=auto to your input elements:

Use dir="auto" on forms and inserted text in order to automatically detect the direction of content supplied at run-time.

https://www.w3.org/International/questions/qa-html-dir#quickanswer

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