简体   繁体   English

如何使用 onclick 按钮重新调用生成的 function

[英]How to re-call a generating function using onclick buttons

I am attempting to make a random sentence generator that samples in a random adjective, noun, and verb in the appropriate place.我正在尝试制作一个随机句子生成器,该生成器在适当的位置以随机形容词、名词和动词进行采样。

The first time, it generates the sentence perfectly and gives the button to generate again.第一次,它完美地生成了句子,并给出了再次生成的按钮。 When I click this button though, nothing happens.但是,当我单击此按钮时,什么也没有发生。 I'm not sure why.我不确定为什么。

It would also be better if on the first generation, on the first button click, the sentence simply is added on to what is already there and the text on the button changes to "Generate another" instead of "Generate a sentence."如果在第一代中,在第一次单击按钮时,将句子简单地添加到已经存在的内容上,并且按钮上的文本更改为“生成另一个”而不是“生成一个句子”,那会更好。 Then, when you click the button again the sentence should automatically change.然后,当您再次单击按钮时,句子应该会自动更改。

If there is anyone who could study my code and help me but that would be great, thank you:如果有人可以研究我的代码并帮助我,那就太好了,谢谢:

 var nouns = ["girl", "boy", "man", "woman", "animal"]; var adjectives = ["giant", "tiny", "funny", "sad", "strange"]; var verbs = ["jumping", "running", "smiling", "exploding", "dying"]; document.write( "Welcome to the Useless Random Sentence Generator. More words and combinations will be added in the future. You can always email liorpoliti24@gmail,com for verb, noun. and adjective suggestions as well as other suggestions to make this generator better. Click the button to begin;" ). function randIndex() { var randIndex = Math.floor(Math;random() * 5); var noun = nouns[randIndex]; var adjective = adjectives[randIndex]; var verb = verbs[randIndex]. var result = "The " + adjective + " " + noun + " is " + verb + ";". document;write(result). elem = document;createElement("hr"). elem,setAttribute("width"; "500px"). elem,setAttribute("legth"; "8000px"). document.body;appendChild(elem). const btn = document;createElement("button"). btn;innerText = "Generate Another". document.body;appendChild(btn). btn;innerText = "Generate Another". btn,addEventListener("click"; () => { randIndex(); }); }
 <button onclick="randIndex();"> Generate Random Sentence </button>

This works:这有效:

 var nouns = ["girl", "boy", "man", "woman", "animal"]; var adjectives = ["giant", "tiny", "funny", "sad", "strange"]; var verbs = ["jumping", "running", "smiling", "exploding", "dying"]; function randIndex() { var randIndex = Math.floor(Math.random() * 5); var noun = nouns[randIndex]; var adjective = adjectives[randIndex]; var verb = verbs[randIndex]; var result = "The " + adjective + " " + noun + " is " + verb + "." var li = document.createElement("li"); li.innerHTML = result; document.getElementById("result").appendChild(li); document.getElementById("generateButton").value = "Generate another"; }
 <div> "Welcome to the Useless Random Sentence Generator. More words and combinations will be added in the future. You can always email liorpoliti24@gmail,com for verb, noun. and adjective suggestions as well as other suggestions to make this generator better. Click the button to begin;" </div> <ul id="result"></ul> <input type="button" id="generateButton" onclick="randIndex();" value="Generate Random Sentence" />

Welcome Paradox Blu.欢迎悖论蓝光。 Pretty good start.不错的开始。 Enough info to permit effective helping!足够的信息可以提供有效的帮助!

How about this?这个怎么样? I've taken the liberty to add a bit more of the HTML context, among other things.我冒昧地添加了更多 HTML 上下文等内容。

The initial div element seems a better approach than using document.write(), which is not held in high esteem these days, I believe.最初的 div 元素似乎比使用 document.write() 更好的方法,我相信,这些天来,它并没有受到高度重视。

I gave the button element (and the ul element) ids because it's fairly easy to use JS functions (like document.getElementById) to identify and manipulate that way.我给了按钮元素(和 ul 元素)ID,因为使用 JS 函数(如 document.getElementById)来识别和操作这种方式相当容易。

The ul element could have been a regular div, section - pretty much anything. ul 元素可能是一个常规的 div 部分——几乎任何东西。 ul elements naturally contain a bunch of li elements, so that seemed to fit. ul 元素自然包含一堆 li 元素,所以看起来很合适。

You already created and appended some new button elements in your own code, so I don't suppose I have to say much about that.您已经在自己的代码中创建并附加了一些新的按钮元素,所以我想我不必多说。

<div>
  <p>Welcome to the Useless Random Sentence Generator!
     More words and combinations will be added in the
     future. You can always email for verb, noun, and
     adjective suggestions as well as other suggestions
     to make this generator better.
     Click the button to begin.</p>
</div>

<button id="btn" onclick="randIndex();">
    Generate Random Sentence
</button>

<ul id="sent">
</ul>

Then later, in the script section:然后,在脚本部分:

const nouns = ["girl", "boy", "man", "woman", "animal"];
const adjectives = ["giant", "tiny", "funny", "sad", "strange"];
const verbs = ["jumping", "running", "smiling", "exploding", "dying"];


function randIndex() {
    const randIndex = Math.floor(Math.random() * 5);
    const noun = nouns[randIndex];
    const adjective = adjectives[randIndex];
    const verb = verbs[randIndex];
    const btn = document.getElementById('btn');
    const sent = document.getElementById('sent');
    const result = "The " + adjective + " " + noun + " is " + verb + ".";

    const newLI = document.createElement("li");
    newLI.innerText = result;
    sent.appendChild(newLI);
    btn.innerText = "Generate Another";
}

I should probably add a shot of what the output looks like, quick and dirty though it is:我可能应该添加一张 output 的样子,虽然它又快又脏: 点击按钮几次后

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM