2015-05-19 184 views
1

我想用JavaScript和jQuery动态创建一个按钮。Javascript - 动态创建按钮

下面的代码是测验的最后一部分,当测验完成时,它会给出结果并说明您已完成。我想在这段时间里使用JS动态地创建一个按钮,并将用户带回到主页(index.html)。

任何帮助,非常感谢。

function displayFinalSlide(){ 

    $(stage).append('<div class="questionText">You have finished the quiz!<br><br>Total questions: '+numberOfQuestions+'<br>Correct answers: '+score+'</div>'); 

}//display final slide 
+3

采取猜测将以下代码!你来了这么远!该声明为文档添加了一些HTML。你想添加更多的HTML;你需要标记来添加一个按钮/锚点。 – Luke

+0

只需添加按钮的标记。 – Mairaj

回答

2

试试这个:就像您添加的HTML,只需添加锚标记/按钮以及

function displayFinalSlide(){ 

    $(stage).append('<div class="questionText">You have finished the quiz!<br><br>Total questions: '+numberOfQuestions+'<br>Correct answers: '+score+'</div><a href="index.html">Index Page</a>'); // here in href you can give the path where you want to redirect to 

} 
0

你可以创建一个不可见的按钮,让你回到主页:

<button id="finalButton" style="visibility:hidden">Go back</button> 

然后在函数 “displayFinalSlide()” 以上:

$('#finalButton').show(); 

我已经省略了按钮的jquery点击返回到主站点。

0

你可以在你的代码

function displayFinalSlide(){ 
$(stage).append('<div class="questionText">You have finished the quiz!<br> <br>Total questions: '+numberOfQuestions+'<br>Correct answers: '+score+'</div> 
<br/><button type="button" id="goback"> Home </button> '); } }); $("#goback").on("click",function(){window.location.assign('index.html');})