我需要重构javascript代码(使其更好、更小)
我有一个html 用这种结构我需要重构javascript代码(使其更好、更小),javascript,jquery,event-handling,refactoring,Javascript,Jquery,Event Handling,Refactoring,我有一个html 用这种结构 <table id="position_holder"> <tr> <td><a href="#">link1</a> <div class="question">question1</div> <div class="question">question2</div> &
<table id="position_holder">
<tr>
<td><a href="#">link1</a>
<div class="question">question1</div>
<div class="question">question2</div>
</td>
</tr>
<tr>
<td><a href="#">link2</a>
<div class="question">question3</div>
<div class="question">question4</div>
<div class="question">question5</div>
<div class="question">question6</div>
</td>
</tr>
</table>
这会向用户逐一提问,首先检查是否有更多问题,然后如果用户选择“chancel”并发出错误警报,则测试结束,如果用户选择“ok”,则进入下一个问题
请帮我做这个更方便的方式,我认为这个代码是可怕的 用这个。这基本上是做同样的事情,因为所讨论的代码将减少字符数
$('#position_holder td a').on('click', function() {
if (!$(this).next('.question').text()) {
alert('No test yet!');
} else {
var $questions = $(this).closest('td').find('.question');
var questionCount = $questions.length;
var count = 0;
// Loop through all the questions
$questions.each(function() {
if (confirm($(this).text())) {
if (++count === questionCount)
alert('TestSuccess!');
} else {
alert('Test Failure!');
return false; // will break out of `each`
}
});
}
});
用这个。这基本上是做同样的事情,因为所讨论的代码将减少字符数
$('#position_holder td a').on('click', function() {
if (!$(this).next('.question').text()) {
alert('No test yet!');
} else {
var $questions = $(this).closest('td').find('.question');
var questionCount = $questions.length;
var count = 0;
// Loop through all the questions
$questions.each(function() {
if (confirm($(this).text())) {
if (++count === questionCount)
alert('TestSuccess!');
} else {
alert('Test Failure!');
return false; // will break out of `each`
}
});
}
});
用这个。这基本上是做同样的事情,因为所讨论的代码将减少字符数
$('#position_holder td a').on('click', function() {
if (!$(this).next('.question').text()) {
alert('No test yet!');
} else {
var $questions = $(this).closest('td').find('.question');
var questionCount = $questions.length;
var count = 0;
// Loop through all the questions
$questions.each(function() {
if (confirm($(this).text())) {
if (++count === questionCount)
alert('TestSuccess!');
} else {
alert('Test Failure!');
return false; // will break out of `each`
}
});
}
});
用这个。这基本上是做同样的事情,因为所讨论的代码将减少字符数
$('#position_holder td a').on('click', function() {
if (!$(this).next('.question').text()) {
alert('No test yet!');
} else {
var $questions = $(this).closest('td').find('.question');
var questionCount = $questions.length;
var count = 0;
// Loop through all the questions
$questions.each(function() {
if (confirm($(this).text())) {
if (++count === questionCount)
alert('TestSuccess!');
} else {
alert('Test Failure!');
return false; // will break out of `each`
}
});
}
});
您可以这样做:
$('#position_holder td a').on('click', function(){
var question = $(this).next('.question');
// check if the question has text
if(!question.text()){
alert('No test yet!');
return;
}
// loop until test success or test failure
while(true) {
// confirm current question
if(confirm(question.text())) {
// take next question
question = question.next('.question');
// success if no text
if(!question.text()){
alert('TestSuccess!');
break;
}
// continue if there is text
continue;
}
// stop if confirm was canceled
alert('Test Faluture');
break;
}
});
演示:您可以执行以下操作:
$('#position_holder td a').on('click', function(){
var question = $(this).next('.question');
// check if the question has text
if(!question.text()){
alert('No test yet!');
return;
}
// loop until test success or test failure
while(true) {
// confirm current question
if(confirm(question.text())) {
// take next question
question = question.next('.question');
// success if no text
if(!question.text()){
alert('TestSuccess!');
break;
}
// continue if there is text
continue;
}
// stop if confirm was canceled
alert('Test Faluture');
break;
}
});
演示:您可以执行以下操作:
$('#position_holder td a').on('click', function(){
var question = $(this).next('.question');
// check if the question has text
if(!question.text()){
alert('No test yet!');
return;
}
// loop until test success or test failure
while(true) {
// confirm current question
if(confirm(question.text())) {
// take next question
question = question.next('.question');
// success if no text
if(!question.text()){
alert('TestSuccess!');
break;
}
// continue if there is text
continue;
}
// stop if confirm was canceled
alert('Test Faluture');
break;
}
});
演示:您可以执行以下操作:
$('#position_holder td a').on('click', function(){
var question = $(this).next('.question');
// check if the question has text
if(!question.text()){
alert('No test yet!');
return;
}
// loop until test success or test failure
while(true) {
// confirm current question
if(confirm(question.text())) {
// take next question
question = question.next('.question');
// success if no text
if(!question.text()){
alert('TestSuccess!');
break;
}
// continue if there is text
continue;
}
// stop if confirm was canceled
alert('Test Faluture');
break;
}
});
演示:您好,我在您的html中找不到“position”holder li,但这是我根据您的html创建的脚本
$('table a').on('click', function () {
$(this).parents("td").find(".question").each(function (index) {
if(!confirm($(this).text())){
alert('Test Failure');
return false;
}else{
alert('Test Success !');
}
});
return false;
});
工作演示您好,我在您的html中找不到#position_holder li,但这是我根据您的html创建的脚本
$('table a').on('click', function () {
$(this).parents("td").find(".question").each(function (index) {
if(!confirm($(this).text())){
alert('Test Failure');
return false;
}else{
alert('Test Success !');
}
});
return false;
});
工作演示您好,我在您的html中找不到#position_holder li,但这是我根据您的html创建的脚本
$('table a').on('click', function () {
$(this).parents("td").find(".question").each(function (index) {
if(!confirm($(this).text())){
alert('Test Failure');
return false;
}else{
alert('Test Success !');
}
});
return false;
});
工作演示您好,我在您的html中找不到#position_holder li,但这是我根据您的html创建的脚本
$('table a').on('click', function () {
$(this).parents("td").find(".question").each(function (index) {
if(!confirm($(this).text())){
alert('Test Failure');
return false;
}else{
alert('Test Success !');
}
});
return false;
});
工作演示它将检查所有的
。问题
类div也跳过第一个问题。如果所有问题都得到确认,则应该提醒成功,而不是一个问题没有确认。@Mitul感谢您的通知。请查看更新的答案,问题仍然与我之前的评论相同。您跳过了第一个问题,如果问题未得到确认,您仍在呼叫成功。越来越近<代码>如果(!confirm($(this).下一个('.question').text()){指的是第二个问题。如果所有问题都已确认,则应调用“测试成功”。因此,您需要如果(confirm($(this).text())
。如果有一个问题未确认(其他情况)您将显示测试失败警报并结束循环。它将检查所有。问题类div还将跳过第一个问题。如果所有问题都已确认,则应发出成功警报,而不是一个问题未确认。@Mitul感谢通知。请检查更新的答案仍然与我之前的评论中的问题相同。您将跳过第一个问题如果问题未得到确认,您仍在调用success。越来越近。if(!confirm($(this)。next('.question').text()){
指的是第二个问题。如果所有问题都得到确认,则应调用“Test Successful”。因此,您需要if(confirm($(this).text())
。如果有一个问题未得到确认(其他情况)您将显示测试失败警报并结束循环。它将检查所有。问题类div还将跳过第一个问题。如果所有问题都已确认,则应发出成功警报,而不是一个问题未确认。@Mitul感谢通知。请检查更新的答案仍然与我之前的评论中的问题相同。您将跳过第一个问题如果问题未得到确认,您仍在调用success。越来越近。if(!confirm($(this)。next('.question').text()){
指的是第二个问题。如果所有问题都得到确认,则应调用“Test Successful”。因此,您需要if(confirm($(this).text())
。如果有一个问题未得到确认(其他情况)您将显示测试失败警报并结束循环。它将检查所有。问题类div还将跳过第一个问题。如果所有问题都已确认,则应发出成功警报,而不是一个问题未确认。@Mitul感谢通知。请检查更新的答案仍然与我之前的评论中的问题相同。您将跳过第一个问题如果问题未得到确认,您仍在调用success。越来越近。if(!confirm($(this)。next('.question').text()){
指的是第二个问题。如果所有问题都得到确认,则应调用“Test Successful”。因此,您需要if(confirm($(this).text())
。如果有一个问题没有得到确认(否则),您将显示测试失败警报并结束循环。那么向用户发送确认消息呢?他必须取消回答所有问题it@user14511我已经添加了确认消息,并在演示代码中更新了新链接,现在它在通过的每个测试中都有“测试成功”的消息。如果需要,最后一个else必须与else一起重播($(this).next().text()=''{alert('Test Success!');}那么给用户的确认消息呢?他必须取消对所有消息的应答it@user14511我已经添加了确认消息,并在演示代码中更新了新链接,现在它在通过的每个测试中都有“测试成功”。最后一个else必须与else if($(this).next().text()='')一起重播{alert('testsuccess!');}那么给用户的确认消息呢?他必须取消对所有消息的应答it@user14511我已经添加了在演示代码中更新的确认消息新链接,现在它在通过的每个测试中都有“测试成功”。最后一个else必须与else if($(this).next().text()='')一起重播{alert('testsuccess!';}那么给用户的确认消息呢?他必须取消对所有消息的应答it@user14511我已经添加了确认消息,并在演示代码中更新了新链接,现在它在通过的每个测试中都有“测试成功”。最后一个else必须与else if($(this).next().text()=''){alert('test success!');}