Javascript函数未启动,I';I’我不知道怎么修理?

Javascript函数未启动,I';I’我不知道怎么修理?,javascript,function,Javascript,Function,我正在编写我的第一个Javascript函数,在这个函数中,用户可以用提示符键入文本,用户说他们希望用提示符替换文本中的哪些单词,最终新文本将以html显示 但是该功能没有启动(例如,甚至没有弹出提示。我确信这是一个巨大的初学者错误,但我就是想不出来。希望有人能在这里帮助我 函数wordReplacer(){ 函数originalTextPrompt(){ var originalText=prompt(“在此处写入文本”) 函数字ToBeReplacedCompt(){ var wordto

我正在编写我的第一个Javascript函数,在这个函数中,用户可以用提示符键入文本,用户说他们希望用提示符替换文本中的哪些单词,最终新文本将以html显示

但是该功能没有启动(例如,甚至没有弹出提示。我确信这是一个巨大的初学者错误,但我就是想不出来。希望有人能在这里帮助我

函数wordReplacer(){
函数originalTextPrompt(){
var originalText=prompt(“在此处写入文本”)
函数字ToBeReplacedCompt(){
var wordtobereplace=prompt(“键入要替换的单词”)
函数newWordPrompt(){
var newWord=prompt(“您希望用什么词替换上一个词?”)
var txt=原始文本;
txt=txt.replace(wordtobereplace,newWord);
document.getElementById('WordReplaceId')。innerHTML=txt;
}

单击此处转换文本

请尝试以下代码。实现所需行为不需要太多函数

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;
}
我认为你可以花一些时间在更好的工具上。 花5分钟检查论文:


声明两个子函数,但不关闭大括号或调用它们。 更好:

函数wordReplacer(){
var originalText=prompt(“在此处写入文本”);
var wordtobereplace=提示(“键入要替换的单词”);
var newWord=prompt(“您希望用什么词替换上一个词?”);
var txt=原始文本;
txt=txt.replace(wordtobereplace,newWord);
document.getElementById('WordReplaceId')。innerHTML=txt;
}

单击此处转换文本

您只调用了
wordReplacer()
,但在这个函数中,有一个函数层次结构

wordReplacer()
中有
originalTextPrompt()
未被调用,这就是您无法获得任何输出的原因

您正在执行太多不需要的功能层次结构

试试下面的代码

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;
}

这是显示如何使用函数的第二个答案

函数wordReplacer(){
函数originalTextPrompt(){
返回提示(“在此处写入文本”);
}
函数字ToBeReplacedCompt(){
返回提示(“键入要替换的单词”);
}
函数newWordPrompt(){
返回提示(“您希望用什么单词替换上一个单词?”);
}
var originalText=originalTextPrompt();
var wordtobereplace=wordtobereplacedcompt();
var newWord=newWordPrompt();
var txt=原始文本;
txt=txt.replace(wordtobereplace,newWord);
document.getElementById('WordReplaceId')。innerHTML=txt;
}

单击此处转换文本

格式化代码肯定会有助于发现明显的语法错误,即缺少右大括号。我试图格式化代码,我认为它们是嵌套函数,如果错误,请随意还原是的,嵌套函数没有被关闭,也没有被调用。我想还有更多代码需要粘贴。同意,有3个您可能不需要的内部函数,请检查并告诉我们更多省略分号有什么问题?代码可能有点含糊不清。因为书中有关于自动分号插入(引擎如何决定在何处插入分号)的整章内容我不会抓住机会,希望引擎能够理解我的意图,并始终将它们放在正确的位置。我知道有点冗长。但在您了解丢失的分号将在何处产生导致错误x00行的模糊性之前,您必须找到这一点,您不应该遗漏分号。
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;
      }
    }
  }
}