use text-align smartly (if english dir=ltr if arabic dir=rtl)(巧妙地使用文本对齐(如果英语 dir=ltr 如果阿拉伯语 dir=rtl))
问题描述
我有一个社区网站,我希望用户写
I have a community web site and I want that users write
- 带有方向 LTR/text-align: left) 的英文帖子和
- 带有方向 RTL/text-align: right 的阿拉伯语帖子.
例如Google+ 和 twitter 提供了这样的 POST 解决方案.
E.g. Google+ and twitter provides such an POST solution.
当我从 rtl 或 ltr 中的数据库 post load 读取它时,我想自动添加方向属性到 post !但我不知道怎么做?!
I want add automatically direction attribute to post when i read it from data base post load in rtl or ltr ! but i don't know how ?!
推荐答案
您需要创建一个函数,其中包含您知道的所有字母 RTL 并在加载时检查.要显示 RTL,您需要 CSS 属性、direction、text-align 和 unicode-bidi.
You'll need to create a function that has all the letters you know are RTL and check when loading. To display RTL you need the CSS attributes, direction, text-align, and unicode-bidi.
演示:
function checkRtl( character ) {
var RTL = ['ا','ب','پ','ت','س','ج','چ','ح','خ','د','ذ','ر','ز','ژ','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ک','گ','ل','م','ن','و','ه','ی'];
return RTL.indexOf( character ) > -1;
};
var divs = document.getElementsByTagName( 'div' );
for ( var index = 0; index < divs.length; index++ ) {
if( checkRtl( divs[index].textContent[0] ) ) {
divs[index].className = 'rtl';
} else {
divs[index].className = 'ltr';
};
};
CSS
.rtl {
direction: rtl;
text-align: right;
unicode-bidi: bidi-override;
}
.ltr {
direction: ltr;
text-align: left;
unicode-bidi: bidi-override;
}
HTML
<div>hello</div>
<div>ظ</div>
这篇关于巧妙地使用文本对齐(如果英语 dir=ltr 如果阿拉伯语 dir=rtl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:巧妙地使用文本对齐(如果英语 dir=ltr 如果阿拉伯语 dir=rtl)
基础教程推荐
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
