2014-04-13 36 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