我正在学习JavaScript,想知道为什么像这样的代码:
document.getElementById('partofid'+variable+number)
不能正常工作?
下面是示例和JSfiddle链接,我希望“下一个”按钮可以删除当前显示的项目并显示下一个项目。
HTML:
<div id="div-1"> 1 </div>
<div id="div-2" style="display: none"> 2 </div>
<div id="div-3" style="display: none"> 3 </div>
<div id="div-4" style="display: none"> 4 </div>
<a id="next" href="#">next</a>
JS:
var counter = 1;
var button = document.getElementById('next');
button.addEventListener("click",function(){
var currentDiv = document.getElementById('div-'+counter);
currentDiv.remove();
var nextDiv = document.getElementById('div-'+counter+1);
alert(nextDiv); // why does it return null
alert('div-'+counter+1); // while this doesn't?
nextQuestion.style.display = "block";
counter++;
},true);
string + 1 + 1 === string11
- adeneovar string = "string"
,否则将变量声明为var string = '1'
。 - gillyspy111
。 - adeneovar string = 1
这句话,意味着答案将是3,但是时间编辑器已经用完了。我的意思是你弄混了隐喻,现在我说你在“混淆隐喻”,反而混淆了隐喻。 - gillyspy