0

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

Ps 我确实在网上找到了这段代码,但它似乎很虚伪。

<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>
4

2 回答 2

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;
}
于 2013-05-08T22:42:25.537 回答
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;

};
于 2013-05-08T22:57:55.667 回答