2013-05-08 52 views
0

我對JavaScript很陌生,但正在嘗試編寫一個將文本數字轉換爲圖像(數字)的腳本。我該怎麼辦?將數字0-9轉換爲圖像?

P.s.我確實在網上找到了這個代碼,但它似乎很希望。

<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> 
+0

只是一個問題,你爲什麼不想做呢? – unkn0wn 2013-05-08 22:41:30

+0

我在猜測captcha或類似的東西? – adeneo 2013-05-08 22:45:13

+2

@adeneo很明顯,做出一個價格;我猜想user2364094想要實現類似於[this](http://riii.nl/w9349)的事情(繼續,更改url中的數字)。 – RobIII 2013-05-08 22:48:52

回答

1
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; 
} 
+1

我會確保input是一個字符串,而不是float/integer,所以'.substring()'和'.length'不會失敗。只需添加一個簡單的'input = input.toString();'或者'input =''+ input;'或者循環之前的東西。這只是爲了處理某人調用像phils_image_price(123.45);'而不是所謂的phils_image_price('123.45');''的函數。另一種選擇是「嗅探」傳遞的int/float,以確保在需要時也調用'.toFixed(2)'。只是一個'防守編程',沒有什麼壯觀的;) – RobIII 2013-05-08 22:51:23

+0

所以我在Wordpress中使用它。我已經把代碼放在了一個js文件中,放在了functions.php中,我怎麼在wordpress的頁面上調用函數呢?謝謝 :) – wackerfuss 2013-05-09 03:28:01

1
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; 

};