当您访问Facebook并想要发表评论时,关于您为键盘选择的语言,当您点击文本框撰写评论时,文本框的方向会自动更改。如果您为键盘选择了阿拉伯语,文本框将自动获取RTL方向。 任何想法如何实现?我想在我的网络应用程序中使用它。
非常感谢您的帮助,我非常感谢。
您可以使用CSS direction 属性实现这一点: 输入{ direction:rtl; }
更新
要根据用户输入动态更改文本的方向,您可以检查输入的第一个字符,以查看其是否符合给定的条件,在这种情况下是正则表达式。
$('input')。keyup(function(){ $ this = $(this); if this.val()。length == 1) { var x = new RegExp([\x00-\x80] +); // is ascii // alert(x.test($ this.val())); var isAscii = x.test($ this.val()); if(isAscii) { $ this.css(direction,ltr); } else { $ this.css (direction,rtl); } } });这是一个使用 ltr ascii文本的方向和 rtl 。
这是一个工作示例。
When you go to Facebook and want to leave a comment, regarding which language you have chosen for your keyboard the direction of the textbox changes automatically when you click on it to write your comment. If you have chosen Arabic language for your keyboard the textbox automatically gets RTL direction. any idea how it has been implemented? I would like to use it on my web application.
thank you very much for your help, I really appreciate it.
regards
解决方案You can use the CSS direction property to achieve this:
input{ direction: rtl; }Update
To change the direction of the text dynamically based on the user input you can check the first character of input to see if it meets a given criteria, in this case a regular expression.
$('input').keyup(function(){ $this = $(this); if($this.val().length == 1) { var x = new RegExp("[\x00-\x80]+"); // is ascii //alert(x.test($this.val())); var isAscii = x.test($this.val()); if(isAscii) { $this.css("direction", "ltr"); } else { $this.css("direction", "rtl"); } } });This is a basic example that uses ltr direction for ascii text and rtl for everything else.
Here's a working example.
更多推荐
自动更改文本框的文本方向
发布评论