use text-align smartly (if english dir=ltr if arabic dir=rtl)

后端 未结 5 1414
北海茫月
北海茫月 2020-12-02 19:45

I have a community web site and I want that users write

  • English posts with direction LTR / text-align: left) and
  • Arabic posts with direction RTL / tex
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 20:15

    Actually the Arabic alphabet letters are these

    var RTL = ['ا','ء','ب','ت','ث','ج','ح','خ','د','ذ','ر','ز','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ک','ل','م','ن','و','ه','ی'];
    

    and Persian (Farsi) alphabet letters are these

    var RTL = ['ا','ب','پ','ت','ث','ج','چ','ح','خ','د','ذ','ر','ز','ژ','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ک','گ','ل','م','ن','و','ه','ی'];
    

    and it can be useful if check the first and second letter because sometimes it can starts by a bullet or something like that.

    for ( var index = 0; index < divs.length; index++ ) {
        if( checkRtl( divs[index].textContent[0] ) || checkRtl( divs[index].textContent[1] ) ) {
            divs[index].className = 'rtl';
        } else {
            divs[index].className = 'ltr';
        };
    };
    

提交回复
热议问题