2011-09-09 43 views
-2

有人可以幫我將下面的代碼改寫成Javascript?將PHP代碼重寫爲Javascript

public function replace($text) 
{ 
    $array = array(
     '1' => '1.png', 
     '2' => '2.png' 
    ); 

    reset($array); 
    while (list($code, $path) = each($array)) 
     $text = str_replace($code, "<img src=\"/img/$path\" />", $text); 

    return $text; 
} 
+1

http://www.w3schools.com/jsref/default.asp或使用像prototypejs或jQuery的一個框架和讀出的文檔。如果你知道PHP,你可以做或多或少的JavaScript。 – pduersteler

+0

感謝您的快速回答。 –

回答

1

甲逐字溶液:

function replace(text) { 
    var array = { 
    1: '1.png', 
    2: '2.png' 
    }; 
    for (code in array) { 
    var path = array[code]; 
    text = text.replace(code, '<img src="/img/' + path + '" />'); 
    } 
    return text; 
} 
+0

謝謝! (22222) –