简体   繁体   English

Javascript 功能无法启动,我不知道如何解决?

[英]Javascript function does not start and I'm not sure how to fix?

I'm writing my first Javascript function where user can type in text with Prompt, user says which words in the text they would like to replace with prompt and eventually the new text gets shown in html.我正在编写我的第一个 Javascript 函数,用户可以在其中使用 Prompt 输入文本,用户说他们想用提示替换文本中的哪些词,最终新文本显示在 html 中。

The function doesn't start though (eg there's not even a prompt popping up. I'm sure it's a huge beginner mistake but I just cant figure it out. Hope someone can help me out here!但是该功能没有启动(例如,甚至没有弹出提示。我确定这是一个巨大的初学者错误,但我无法弄清楚。希望有人能在这里帮助我!

 function wordReplacer() { function originalTextPrompt() { var originalText = prompt("Write your text here") function wordToBeReplacedPrompt() { var wordToBeReplaced = prompt("Type the word to be replaced") function newWordPrompt() { var newWord = prompt("With what word would you like the previous word to be replaced?") var txt = originalText; txt = txt.replace (wordToBeReplaced, newWord); document.getElementById('wordreplacerid').innerHTML = txt; }
 <button type="button" onclick="wordReplacer()"> Click here to convert text </button> <p id="wordreplacerid"></p>

I expected a prompt to start, but it seems like the function doesn't run.我希望有一个提示开始,但该功能似乎没有运行。

Please try the following code.请尝试以下代码。 You dont need so many functions for achieve your desired behavior你不需要这么多功能来实现你想要的行为

function wordReplacer() {
    var originalText = prompt("Write your text here")

    var wordToBeReplaced = prompt("Type the word to be replaced")


    var newWord = prompt("With what word would you like the previous word to be replaced ? ")   
    var txt = originalText; 
    txt = txt.replace(wordToBeReplaced, newWord); 
    document.getElementById('wordreplacerid').innerHTML = txt;
}
function wordReplacer() {
  function originalTextPrompt() {
    var originalText = prompt("Write your text here")
    function wordToBeReplacedPrompt() {
      var wordToBeReplaced = prompt("Type the word to be replaced")
      function newWordPrompt() {
        var newWord = prompt("With what word would you like the previous word to be replaced?") 
        var txt = originalText;
        txt = txt.replace (wordToBeReplaced, newWord);
        document.getElementById('wordreplacerid').innerHTML = txt;
      }
    }
  }
} //Adding theses

I think you can spend some time on a better tooling.我认为您可以花一些时间在更好的工具上。 Take 5 min to check theses:花 5 分钟检查论文:

You declare two sub-functions but do not close the braces or call them.您声明了两个子函数,但不要关闭大括号或调用它们。 Better:更好的:

 function wordReplacer() { var originalText = prompt("Write your text here"); var wordToBeReplaced = prompt("Type the word to be replaced"); var newWord = prompt("With what word would you like the previous word to be replaced?"); var txt = originalText; txt = txt.replace (wordToBeReplaced, newWord); document.getElementById('wordreplacerid').innerHTML = txt; }
 <button type="button" onclick="wordReplacer()"> Click here to convert text </button> <p id="wordreplacerid"></p>

You are only calling wordReplacer() but inside this function, there is a hierarchy of functions.你只是调用wordReplacer()但在这个函数内部,有一个函数层次结构。

Inside wordReplacer() there is originalTextPrompt() which is not called that's why you are not getting any output.wordReplacer()originalTextPrompt() ,它没有被调用,这就是为什么你没有得到任何输出。

You are performing too many hierarchy of functions which is not needed.您正在执行太多不需要的功能层次结构。

Try below code.试试下面的代码。

function wordReplacer() {
    var originalText = prompt("Write your text here");
    var wordToBeReplaced = prompt("Type the word to be replaced");
    var newWord = prompt("With what word would you like the previous word to be replaced ? ");
    var txt = originalText; 
    txt = txt.replace(wordToBeReplaced, newWord); 
    document.getElementById('wordreplacerid').innerHTML = txt;
}

This is a second answer showing how to use functions.这是显示如何使用函数的第二个答案。

 function wordReplacer() { function originalTextPrompt() { return prompt("Write your text here"); } function wordToBeReplacedPrompt() { return prompt("Type the word to be replaced"); } function newWordPrompt() { return prompt("With what word would you like the previous word to be replaced?"); } var originalText = originalTextPrompt(); var wordToBeReplaced = wordToBeReplacedPrompt(); var newWord = newWordPrompt(); var txt = originalText; txt = txt.replace (wordToBeReplaced, newWord); document.getElementById('wordreplacerid').innerHTML = txt; }
 <button type="button" onclick="wordReplacer()"> Click here to convert text </button> <p id="wordreplacerid"></p>

function wordReplacer() {
  function originalTextPrompt() {
    var originalText = prompt("Write your text here");
    function wordToBeReplacedPrompt() {
      var wordToBeReplaced = prompt("Type the word to be replaced");
      function newWordPrompt() {
        var newWord = prompt(
          "With what word would you like the previous word to be replaced?"
        );
        var txt = originalText;
        txt = txt.replace(wordToBeReplaced, newWord);
        document.getElementById("wordreplacerid").innerHTML = txt;
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不确定如何继续使用此JavaScript - I'm not sure on how to continue with this JavaScript 我不确定如何将Javascript嵌入到Wordpress页面中 - I'm not sure how to embed Javascript into a Wordpress Page 分派`retrieveSportsDetails`函数,但是我不确定如何写`if`条件 - dispatching the `retrieveSportsDetails` function, but I’m not sure how to write the `if` condition 我在Javascript / ReactJs中破坏了.Bind()。 不确定如何修复 - I broke .Bind() in Javascript/ReactJs. Not sure how to Fix 如何修复我的 Javascript function 不起作用? - How do I fix my Javascript function that does not work? 我正在尝试将TypeScript的示例转换为JavaScript,但不确定如何将其转换 - I'm trying to translate an example of TypeScript into JavaScript, and I'm not sure how to convert it Javascript数组未定义......我不确定为什么 - Javascript array is undefined… and I'm not sure why 如何访问此函数中的数据? 我相信这是一个承诺,但我不确定 - How can I access the data inside this function? I believe it is a promise, but I'm not sure 我不确定如何重置backgroundColor值 - I'm not sure how to reset the backgroundColor value javascript函数以确保字符串的格式为m / d / yyyy - javascript function to make sure string is in the format m/d/yyyy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM