javascript / jquery用于显示自动生成的不同句子

时间:2016-11-21 19:37:33

标签: javascript html

我正在寻找javascript / jquery,它将用新的替换一行....就像有一个输入框输入名称和一个在一行中插入名称并显示innerHTml输出的按钮......

function changeResult(){

	var name = document.getElementById('student_name').value;
	document.getElementById('example').innerHTML = name + ', is a brilliant student';
}
<div align="center">

<input id="student_name" name="name" type="text" value="" size="50"placeholder="Please enter name here" />

<input id="Student_result" name="s-result" type="button" value="Result" onClick="changeResult();" class="myButton"/>
</div>


<div id="example" align="justify">
Henry is a good student.
</div>

为此目的,我使用附加脚本....现在我需要进行更改,就像我点击按钮插入名称它将生成一条线,当我再次点击它生成不同的行等等.. .... 希望有人帮助我...

2 个答案:

答案 0 :(得分:0)

您可以通过多种方式执行此操作,但取决于您希望的结果。这只是向您展示如何构建条件。基本上,这将在每个按钮点击两个文本字符串之间切换。

function changeResult(){
    var name = document.getElementById('student_name').value;
    var example = document.getElementById('example');
    var text = name + ', is a brilliant student';
    if (text == example.value) {
        text = name + ', is a good student';
    }
    example.innerHTML = text;
}

答案 1 :(得分:0)

你需要一个数组,一个你想要的句子列表

var random_sentences = [
  "is a brilliant student",
  "is a full student",
  "is a rude student",
  "is a fat student",
  "is not a student"
 ];

function changeResult(){
  var random_sentence_id = Math.round(Math.random()*(random_sentences.length-1));
  var name = document.getElementById('student_name').value;
  document.getElementById('example').innerHTML = name + ', ' + random_sentences[random_sentence_id];
}
<div align="center">

<input id="student_name" name="name" type="text" value="" size="50"placeholder="Please enter name here" />

<input id="Student_result" name="s-result" type="button" value="Result" onClick="changeResult();" class="myButton"/>
</div>


<div id="example" align="justify">
Henry is a good student.
</div>