如何在具有严格文档类型的 HTML 页面中查看阿拉伯/波斯数字?

发布于 2024-11-01 07:00:39 字数 596 浏览 2 评论 0原文

我有一个从右到左的 HTML 页面。当我不使用任何文档类型时,我的数字是阿拉伯语/波斯语,但当我使用严格模式时,它们会变成英语。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">

添加文档类型之前:

添加文档类型之前 添加

文档类型之后:

添加 doctype

,我还向我的页面添加了这些元标记:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="fa" />

那么,如何在具有严格 doctype 的页面中查看阿拉伯数字(在 IE 中,因为 Firefox 无论如何都不显示阿拉伯数字)?

I have an HTML page that is right-to-left. When I don't use any doctype, my numbers are in Arabic/Persian, but when I use strict mode they turn to English.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">

Before adding doctype:

Before adding doctype

After adding doctype:

After adding doctype

also I added these meta tags to my page:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="fa" />

So how can I view Arabic numbers in a page with strict doctype (in IE because Firefox doesn't show Arabic numbers anyway)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

探春 2024-11-08 07:00:39

您可以在模板中使用以下 JavaScript 代码将英语数字转换为波斯语数字:

    <script type="text/javascript">
    var replaceDigits = function() {
        var map = ["&\#1776;","&\#1777;","&\#1778;","&\#1779;","&\#1780;","&\#1781;","&\#1782;","&\#1783;","&\#1784;","&\#1785;"]
        document.body.innerHTML = document.body.innerHTML.replace(/\d(?=[^<>]*(<|$))/g, function($0) { return map[$0]});
    }
    window.onload = replaceDigits;
    </script>

You can convert English digits to Persian digits using this JavaScript code in your template:

    <script type="text/javascript">
    var replaceDigits = function() {
        var map = ["&\#1776;","&\#1777;","&\#1778;","&\#1779;","&\#1780;","&\#1781;","&\#1782;","&\#1783;","&\#1784;","&\#1785;"]
        document.body.innerHTML = document.body.innerHTML.replace(/\d(?=[^<>]*(<|$))/g, function($0) { return map[$0]});
    }
    window.onload = replaceDigits;
    </script>
扮仙女 2024-11-08 07:00:39

这是一段 javascript 代码,可将 1234 字符串转换为 ١٢٣٤ 字符串

   var map =
    [
    "&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;",
    "&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"
    ];

    function getArabicNumbers(str)
    {
        var newStr = "";

        str = String(str);

        for(i=0; i<str.length; i++)
        {
            newStr += map[parseInt(str.charAt(i))];
        }

        return newStr;
    }

here is a little javascript code that converts a 1234 string to ١٢٣٤ string

   var map =
    [
    "&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;",
    "&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"
    ];

    function getArabicNumbers(str)
    {
        var newStr = "";

        str = String(str);

        for(i=0; i<str.length; i++)
        {
            newStr += map[parseInt(str.charAt(i))];
        }

        return newStr;
    }
变身佩奇 2024-11-08 07:00:39

如果您需要将一些英文替换为阿拉伯数字而不是整个 HTML,请将您需要的数字传递给此函数。

function toArabicNumeral(en) {
    return ("" + en).replace(/[0-9]/g, function(t) {
        return "٠١٢٣٤٥٦٧٨٩".slice(+t, +t+1);
    });
}

In case you need to replace some English to Arabic numerals and not the whole HTML, pass the number you need to this function.

function toArabicNumeral(en) {
    return ("" + en).replace(/[0-9]/g, function(t) {
        return "٠١٢٣٤٥٦٧٨٩".slice(+t, +t+1);
    });
}
空城旧梦 2024-11-08 07:00:39

假设您想要一个 XHTML 1.0 Strict 文档:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fa" lang="fa" dir="rtl">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title here</title>
  </head>

  <body>
    <p>Text here</p>
  </body>
</html>

这是一个等效的 HTML 4.01 Strict 文档:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fa" dir="rtl">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Title here</title>
  </head>

  <body>
    <p>Text here</p>
  </body>
</html>

这是一个等效的 HTML5 页面,仅供比较之用。

<!DOCTYPE html>
<html lang="fa" dir="rtl">
  <head>
    <meta charset="UTF-8" />
    <title>Title here</title>
  </head>

  <body>
    <p>Text here</p>
  </body>
</html>

Assuming you want an XHTML 1.0 Strict document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fa" lang="fa" dir="rtl">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title here</title>
  </head>

  <body>
    <p>Text here</p>
  </body>
</html>

Here's an equivalent HTML 4.01 Strict document:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fa" dir="rtl">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Title here</title>
  </head>

  <body>
    <p>Text here</p>
  </body>
</html>

Here's an equivalent HTML5 page, just for comparison purposes.

<!DOCTYPE html>
<html lang="fa" dir="rtl">
  <head>
    <meta charset="UTF-8" />
    <title>Title here</title>
  </head>

  <body>
    <p>Text here</p>
  </body>
</html>
战皆罪 2024-11-08 07:00:39

如果您使用“BNazanin”等波斯字体并编写:

http-equiv="Content-Type" content="text/html; charset=UTF-8"
和元标记中的 http-equiv="Content-Language" content="fa"

然后您可以看到波斯语的数字。

如果您在 html 页面的某些标记中使用 lang='En' ,则该标记中的数字将以英文显示。

If you use persian fonts like 'BNazanin' and write :

http-equiv="Content-Type" content="text/html; charset=UTF-8"
and http-equiv="Content-Language" content="fa" in meta tags.

You can then see the numbers in persian.

and if you use lang='En' in some tags in your html page the numbers in that tag will be displayed in english.

莫相离 2024-11-08 07:00:39

在 HTML 页面中查看阿拉伯/波斯数字非常简单。

我通过更改字体名称解决了同样的问题,

在我的例子中,我想向所有用户显示相同的字符,

您可以选择包含阿拉伯语-印地语数字的字体并使用 css ( @font-face ) 导入它,然后只需使用它,

这适用于所有主要浏览器(IE .. Firefox .. Chrome)

看看这个结果

这是完整的页面代码:

<html>
<head>

</head>
<body>
<style type="text/css">

@font-face {
    font-family: ArabicTwo_Bold;
    src: url( ArabicTwo_Bold.eot);
}

@font-face {
    font-family: ArabicTwo_Bold;
    src: url( ArabicTwo_Bold.ttf);
}


div , input {
 font-family: ArabicTwo_Bold;
}
</style>


<input type='text' value='هذا نص هندي 123456 ' />
<div> هذا نص هندي 123456  </div>
</body>
</html>

It is very simple to view Arabic/Persian numbers in a HTML page.

I solved the same problem by changing the font name,

In my case I want to display the same character to all users

you can choose a font that contains Arabic-Hndi digits and import it using the css ( @font-face ) then simply use it,

This works fine for all major browsers (IE .. Firefox .. Chrome)

look at this result

here is the full code of the page:

<html>
<head>

</head>
<body>
<style type="text/css">

@font-face {
    font-family: ArabicTwo_Bold;
    src: url( ArabicTwo_Bold.eot);
}

@font-face {
    font-family: ArabicTwo_Bold;
    src: url( ArabicTwo_Bold.ttf);
}


div , input {
 font-family: ArabicTwo_Bold;
}
</style>


<input type='text' value='هذا نص هندي 123456 ' />
<div> هذا نص هندي 123456  </div>
</body>
</html>
故乡的云 2024-11-08 07:00:39

Firefox 默认渲染编号为拉丁文
要更改此设置,请转到 Firefox 的地址栏并输入 about:config
此页面是firefox的高级设置
找到这一行“bidi.numeral”并双击它
如果将值设置为“1”,则 Firefox 会查看上下文来渲染文本。如果文本是波斯语,则将数字渲染为波斯语。
如果将值设置为“4”,则始终将数字呈现为波斯语

firefox default render number to latin
for change this setting go to addressbar of Firefox and type about:config
this page is advanced setting of firefox
find this line "bidi.numeral" and double click on it
if set value to "1" firefox for render text look at context. if text is persian render number to persian.
if set value to "4" alwase render digit number to persian

戒ㄋ 2024-11-08 07:00:39

试试这个,希望对您有帮助;)

在 body 标记之间

<script language="JavaScript" type="text/javascript">


var replaceDigits = function() {
var map =
[
"&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;",
"&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"
]

document.body.innerHTML =
document.body.innerHTML.replace(
/\d(?=[^<>]*(<|$))/g,
function($0) { return map[$0] }
);
}

</script>

插入以下内容:

<script type="text/javascript">
window.onload = replaceDigits
</script>

try this , hope helps you ;)

between and

<script language="JavaScript" type="text/javascript">


var replaceDigits = function() {
var map =
[
"&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;",
"&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"
]

document.body.innerHTML =
document.body.innerHTML.replace(
/\d(?=[^<>]*(<|$))/g,
function($0) { return map[$0] }
);
}

</script>

then in end of body tag insert this :

<script type="text/javascript">
window.onload = replaceDigits
</script>
聚集的泪 2024-11-08 07:00:39

无论文本方向如何(在 Chrome、Safari、Firefox 和 Opera 中),这对我都有效:(

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
            body { direction: rtl; }
        </style>
    </head>

    <body>
        ۴۲
    </body>
</html>

省略 content-language 因为这里不需要。)

This works for me, regardless of the text direction (in Chrome, Safari, Firefox and Opera):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
            body { direction: rtl; }
        </style>
    </head>

    <body>
        ۴۲
    </body>
</html>

(Omitted the content-language since that isn’t necessary here.)

清君侧 2024-11-08 07:00:39
var map_format_arabic = ["&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;", "&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"];

$.each( $('.format_arabic'), function () {
    var n=$(this).text().replace(/\d(?=[^<>]*(<|$))/g, function($0) { return map_format_arabic[$0]});
    $(this).html(n);
});
var map_format_arabic = ["&\#1632;","&\#1633;","&\#1634;","&\#1635;","&\#1636;", "&\#1637;","&\#1638;","&\#1639;","&\#1640;","&\#1641;"];

$.each( $('.format_arabic'), function () {
    var n=$(this).text().replace(/\d(?=[^<>]*(<|$))/g, function($0) { return map_format_arabic[$0]});
    $(this).html(n);
});
白馒头 2024-11-08 07:00:39

此代码支持一位或多位英文数字,​​可以转换为阿拉伯数字:

function toArabicNumber(enNum) {
    if(isNaN(enNum) || enNum == null){ // Check if not a number or null
        return enNum;
    }

    if(typeof enNum == 'string'){ // if it is a string(number) convert it to number
        enNum = Number(enNum);
    }

    if(enNum < 10){
        return "٠١٢٣٤٥٦٧٨٩".substring(enNum, enNum+1);
    }
    else {
        let result = "";
        enNum = enNum + ""; // convert number to string

        for(let i = 0; i < enNum.length; i++){
            let num = Number(enNum[i]); // convert digit by digit to number
            result = result + "٠١٢٣٤٥٦٧٨٩".substring(num, num+1);
        }

        return Number(result);
    }
}

This code supports one or multiple digits of an English number which can be converted to Arabic number:

function toArabicNumber(enNum) {
    if(isNaN(enNum) || enNum == null){ // Check if not a number or null
        return enNum;
    }

    if(typeof enNum == 'string'){ // if it is a string(number) convert it to number
        enNum = Number(enNum);
    }

    if(enNum < 10){
        return "٠١٢٣٤٥٦٧٨٩".substring(enNum, enNum+1);
    }
    else {
        let result = "";
        enNum = enNum + ""; // convert number to string

        for(let i = 0; i < enNum.length; i++){
            let num = Number(enNum[i]); // convert digit by digit to number
            result = result + "٠١٢٣٤٥٦٧٨٩".substring(num, num+1);
        }

        return Number(result);
    }
}
深居我梦 2024-11-08 07:00:39

只需为他们指定一个波斯字体,如“B yekan”即可。
所有的问题都将得到解决。

just specify a persian font like 'B yekan' to them.
all troubleshoots will be solved.

万人眼中万个我 2024-11-08 07:00:39
var replaceDigits = function () {
      var map = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
      document.body.innerHTML = document.body.innerHTML.replace(/\d(?=[^<>]*(<|$))/g, function ($0) {
        return map[$0];
      });
    }
    window.onload = replaceDigits;
var replaceDigits = function () {
      var map = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
      document.body.innerHTML = document.body.innerHTML.replace(/\d(?=[^<>]*(<|$))/g, function ($0) {
        return map[$0];
      });
    }
    window.onload = replaceDigits;
偏爱你一生 2024-11-08 07:00:39

对波斯语字符使用 toLocalString("fa")

Use toLocalString("fa") for Persian characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文