【发布时间】:2015-01-09 09:57:22
【问题描述】:
这里是一个简单测验应用程序的一些代码。一个对象数组用于存储问题、选项和正确答案。每个问题都有四个可能的答案,它们存储在一个数组中。对于这四个选项,我创建了四个动态input radio element。四个输入单选元素中的每一个都分配了一个事件侦听器。但不知何故,事件侦听器没有触发。这可能是什么原因?
为方便起见,我给出了实际问题所在的部分:
var setQues=(function(){
var index=0;
var span=document.createElement('span');
span.setAttribute('style','color:crimson;font-weight:20px;');
span.setAttribute('id','myspan');
var div=document.getElementById('mydiv');
var btn=document.getElementById('btn');
return function (){
div.innerHTML='';
span.innerHTML=ques[index].title;
div.appendChild(span);
div.innerHTML+='</br>';
ques[index].option.forEach(function(el,indx,arr){
var input=document.createElement('input');
input.setAttribute('type','radio');
input.setAttribute('name','question');
input.setAttribute('id',ques[index].option[indx]);
input.value=el;
div.appendChild(input);
var label=document.createElement('label');
label.setAttribute('for',ques[index].option[indx]);
label.innerHTML=el;
div.appendChild(label);
input.addEventListener('change',function(e){
alert(this);
});
div.innerHTML+='</br>';
});
(index < ques.length-1) ? (btn.value='next',index++) : (btn.value='start quiz again',index=0);
}})()
document.getElementById('btn').addEventListener('click',setQues);
完整代码:
var ques=[
{
title:'who is the captain of bangladesh odi team?',
option:['mashrafee','tamim','sakib','mushfic'],
crctAns:'mashrafee'
},
{
title:'who invented ac current?',
option:['nicola tesla','tomas edison','henry ford','graham bell'],
crctAns:'nicola tesla'
},
{
title:'who starred in "TRAINING DAY" movie?',
option:['danzel washington','morgan freeman','lionerdo decaprio','chirstian bell'],
crctAns:'danzel washington'
}
];
var setQues=(function(){
var index=0;
var span=document.createElement('span');
span.setAttribute('style','color:crimson;font-weight:20px;');
span.setAttribute('id','myspan');
var div=document.getElementById('mydiv');
var btn=document.getElementById('btn');
return function (){
div.innerHTML='';
span.innerHTML=ques[index].title;
div.appendChild(span);
div.innerHTML+='</br>';
ques[index].option.forEach(function(el,indx,arr){
var input=document.createElement('input');
input.setAttribute('type','radio');
input.setAttribute('name','question');
input.setAttribute('id',ques[index].option[indx]);
input.value=el;
div.appendChild(input);
var label=document.createElement('label');
label.setAttribute('for',ques[index].option[indx]);
label.innerHTML=el;
div.appendChild(label);
input.addEventListener('change',function(e){
alert(this);
});
div.innerHTML+='</br>';
});
(index < ques.length-1) ? (btn.value='next',index++) : (btn.value='start quiz again',index=0);
}})()
document.getElementById('btn').addEventListener('click',setQues);
#mydiv{
background:lightblue;
border:1px solid skyblue;
width:200px;
height:200px;
box-shadow:-5px 0px
}
input[type="button"]{
cursor:pointer;
}
input[type='radio']{
cursor:pointer;
}
label:hover{
cursor:pointer;
}
<h1>quiz</h1>
<div id='mydiv' ></div>
<input type='button' value='start quiz' id='btn'>
【问题讨论】:
-
您可以先从代码中删除 ** 以便它运行
-
您应该先将事件监听器附加到
radio input,然后再将其附加到您的htmlDOM -
@user3138436 它可以工作,如果您将第一个
**替换为/*并将第二个替换为*/。 -
@mplungjan 是为了在这个网站上把文字加粗。虽然它没有用。我删除了它
-
我从我的代码中删除了 **。这是为了让这个网站使文本变为粗体。它不在我的实际代码中。仍然无法正常工作
标签: javascript html loops event-listener