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)
基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
