Javascript 将数字0-9转换为图像?

Javascript 将数字0-9转换为图像?,javascript,image,numbers,Javascript,Image,Numbers,我对JavaScript非常陌生,但我正在尝试编写一个脚本,将文本数字转换为(数字的)图像。我该怎么办 顺便说一句,我确实在网上找到了这段代码,但它似乎有些含糊不清 <script language="JavaScript1.1" type="text/javascript"> function phils_image_price(input) { //declare variables var output="" var position=0 var chr="" //loop f

我对JavaScript非常陌生,但我正在尝试编写一个脚本,将文本数字转换为(数字的)图像。我该怎么办

顺便说一句,我确实在网上找到了这段代码,但它似乎有些含糊不清

<script language="JavaScript1.1" type="text/javascript">
function phils_image_price(input)
{
//declare variables
var output=""
var position=0
var chr=""
//loop
for (var position=0; position < input.length; position++)
{
chr=input.substring(position,position+1)
//replace
if (chr == '£') {output=output + '<img border="0"
src="img/pound.gif">'}
else if(chr == '0') {output=output + '<img border="0"
src="img/0.gif">'}
else if(chr == '1') {output=output + '<img border="0"
src="img/1.gif">'}
else if(chr == '2') {output=output + '<img border="0"
src="img/2.gif">'}
else if(chr == '3') {output=output + '<img border="0"
src="img/3.gif">'}
else if(chr == '4') {output=output + '<img border="0"
src="img/4.gif">'}
else if(chr == '5') {output=output + '<img border="0"
src="img/5.gif">'}
else if(chr == '6') {output=output + '<img border="0"
src="img/6.gif">'}
else if(chr == '7') {output=output + '<img border="0"
src="img/7.gif">'}
else if(chr == '8') {output=output + '<img border="0"
src="img/8.gif">'}
else if(chr == '9') {output=output + '<img border="0"
src="img/9.gif">'}
else if(chr == '.') {output=output + '<img border="0"
src="img/dot.gif">'}
else {output=output + chr}
}
return output;
}
</script>
</head>
<body>

<script language="JavaScript1.1" type="text/javascript">
document.write('£12345678.90')
document.write('<br>')
document.write(phils_image_price('£12345678.90'))
</script>

函数phils\u图像\u价格(输入)
{
//声明变量
var output=“”
var位置=0
var chr=“”
//环路
对于(变量位置=0;位置){output=output+''}
else如果(chr=='0'){output=output+'''}
else如果(chr=='1'){output=output+'''}
else如果(chr=='2'){output=output+'''}
如果(chr=='3'){output=output+'''}
else如果(chr=='4'){output=output+'''}
else如果(chr=='5'){output=output+'''}
else如果(chr=='6'){output=output+'''}
else如果(chr=='7'){output=output+'''}
else如果(chr=='8'){output=output+'''}
else如果(chr=='9'){output=output+'''}
else如果(chr=='.'){output=output+'''}
else{output=output+chr}
}
返回输出;
}
文件。书写(12345678.90英镑)
文档。写入(“
”) 文件。书写(phils_image_price('12345678.90英镑'))
功能图\u图像\u价格(输入){
var output=“”
对于(变量i=0;i
功能文本数字到图像(文本){
var输出=“”;
//您可以在此处定义文件名列表
var images=['0.gif'、'1.gif'、'2.gif'、'3.gif',
“4.gif”、“5.gif”、“6.gif”、“7.gif”、“8.gif”、“9.gif”];
//我们消除了所有不是数字的字符
var nums=text.replace(/\D/g');
//现在我们迭代所有这些数字
对于(变量i=0;i
只是一个问题,你为什么要这样做?我猜是验证码还是类似的东西?@adeneo这显然是为了定价;我猜user2364094想要实现类似的目标(继续,更改url中的数字)。如果不是验证码,用透明的PNG覆盖文本有什么错?我只看到了robi的链接。这很好,我会记住。我会确保输入是字符串,而不是浮点/整数,所以
.substring()
.length
不会失败。只需添加一个简单的
input=input.toString()“;”或
input=''+input;`或循环之前的某个东西。这只是为了处理调用函数的人,比如
phils\u image\u price(123.45);
而不是假定的
phils\u image\u price('123.45');
。另一个选项是“嗅探”传递的int/float,以确保您也调用
。toFixed(2)
需要时。只是一点“防御性编程”,没什么了不起的;)所以我在Wordpress中使用它。我已经将代码放在functions.php中的js文件中,如何在Wordpress的页面上调用该函数?谢谢:)
function phils_image_price(input) {
    var output = ""

    for (var i = 0; i < input.length; i++) {
        var chr = input.substring(i, i + 1)
        if (chr == '£') {
            output += '<img border="0" src="img/pound.gif">';
        } else if (chr == '.') {
            output += '<img border="0" src="img/dot.gif">';
        } else {
            output += '<img border="0" src="img/'+(chr+1)+'.gif">';
        }
    return output;
}
function textNumbersToImages(text) {

    var output = '';

    // You could define a list of filenames here
    var images = ['0.gif', '1.gif', '2.gif', '3.gif', 
        '4.gif', '5.gif', '6.gif', '7.gif', '8.gif', '9.gif'];

    // We eliminate all characters that are not a number
    var nums = text.replace(/\D/g, ''); 

    // Now we iterate over all those numbers
    for (var i=0; i < nums.length; i++) {
        output += '<img src="' + images[nums.charAt(i)] + '" />';
    }

    // We return the constructed string, that is a list of image tags
    return output;

};