[英]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.