我正在编写我的第一个Javascript函数,在该函数中,用户可以使用Prompt输入文本,用户说出他们想用提示符替换文本中的哪些单词,最终新文本以html显示。
虽然该功能没有启动(例如,甚至没有弹出提示。我敢肯定这是一个巨大的初学者错误,但我只是想不通。希望有人可以在这里帮助我!
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>
我希望启动一个提示,但似乎该功能未运行。
答案 0 :(得分:2)
请尝试以下代码。您不需要太多功能即可实现所需的行为
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;
}
答案 1 :(得分:1)
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
我认为您可以花一些时间在更好的工具上。 花5分钟检查论文:
答案 2 :(得分:0)
您声明了两个子功能,但不关闭括号或调用它们。 更好:
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>
答案 3 :(得分:0)
您仅调用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;
}
答案 4 :(得分:-1)
这是显示如何使用功能的第二个答案。
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>
答案 5 :(得分:-1)
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;
}
}
}
}