2014-04-13 66 views
0

我需要這個,它可以添加一個選項,如分裂和再次加入我不知道這一點,但我看到一個動作腳本與該選項它是「 A,b,C「 這裏是ActionScript代碼,但我想它在JavaScript,請幫我把它...動作腳本到Java腳本 - 字符串到字符串翻譯

tIn.onChanged = function() 
{ 
    var sInput = tIn.text; //Gets the text from the first text field. 
    sInput = translate(sInput); //Goes through the translator. 
    tOut.text = sInput; //Sets the second text field to the translated text. 
} 
function translate(sInput) 
{ 
    //This is where you add how you want your text changed. 
    //It should be in the format of 
    //sInput = searchAndReplace(sInput, What you want changed, What you want it changed    to); 
//Remember to use quotes around the text you are changing. 
    sInput = searchAndReplace(sInput, "how r u sis", "how are you sister "); 
    sInput = searchAndReplace(sInput, "how r u bro", "how are you brother "); 
    sInput = searchAndReplace(sInput, "whatsup ", "what is up "); 
    return sInput; 
} 
function searchAndReplace(a, b, c) //Searches string a for b and replaces it with c 
{ 
    tmp = a.split(b); //Splits a into an array of values where b is. 
    a = tmp.join(c); //Joins them back together with c seperating them. 
    return (a); //Returns the changed string. 
} 
+0

我沒有看到JS中的任何問題,而不是.text,你應該把它放在一個html元素(使用getElementById)。 無論如何你可以縮短它: 'sInput = sInput.split(「怎麼ru sis」)。join(「你好嗎姐妹」).split(「怎麼ru兄弟」)。join(「你好嗎兄弟」 ).split(「whatsup」).join(「what is up」);' – Nadia

+0

嗨@Nadia感謝您的快速答案,但我仍然有一些問題在這裏是代碼:http://raadso.net/tests/ translate.html – OmarTeacher

回答

0

首先...如果你想執行腳本的時候,你鍵入,你有沒有理由添加「搜索」按鈕。 總之,這裏是輸入域代碼(輸入時)和搜索按鈕,選擇你要使用什麼:

<input id="sInput" type="text" onkeydown="translate()"> 
    <input type="button" value="Search" onClick="translate()"> 

比,這個代碼後和格「結果」後,你必須添加javascript代碼:

<script type="text/javascript"> 
    function translate() 
    { 
     var sInput = document.getElementById('sInput').value; 
     sInput = sInput.split("how r u sis").join("how are you sister ").split("how r u bro").join("how are you brother ").split("whatsup ").join("what is up "); 
     document.getElementById('result').innerHTML =sInput; 
    } 
</script>  
+0

如果你想讓它也可以粘貼,請將下面的代碼添加到輸入字段中:onpaste =「translate()」oninput =「translate()」(取決於瀏覽器需要oninputo或onpaste) – Nadia

+0

謝謝Nadia ,你救了我...... – OmarTeacher

+0

我有一個數據庫太多的字符串:word_title和Word_desc,你能幫我連接到這個數據庫嗎?或給我一個例子鏈接,我可以再次感謝納迪亞 – OmarTeacher