나는 이것을 "원숭이 수준"으로 설명하고 어디에서나 찾을 수 없었습니다. 나는 JS를 연구 중이며 페이지 안에 탭을 만들려고했다. W3SCHOOLS 예제에서 코드 조각을 이해할 수 없다는 것을 제외하고는 모든 것이 괜찮다.자바 스크립트 탭 - 루프 설명을 위해
HTML :
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
JS :
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
질문 :
가 for 루프의 목적은 무엇인가
? "i"변수가있는 이유는 무엇입니까? 조건에서 tabcontent/links.length가 사용되는 이유는 무엇입니까?여러분의 도움을 진심으로 감사드립니다. 고맙습니다!
[MDN :'for' 문] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement) – Andreas
그래, 나는 그들을 SoloLearn에서 공부했다. W3schools에. 루프를 정확히 적용해야하는 이유와 i = 0 변수를 사용하는 이유를 모르겠습니다. –
나는이 부분을 제외한 나머지 부분을 아주 잘 이해하고 있습니다. 다른 부분을 읽기 위해서'(i = 0; i;