네이티브 JavaScript 구현 Array.prototype.forEach
, .querySelectorAll
및 .addEventListener
을 사용합니다.
하나의 <a>
<div>
Array.prototype.forEach.call(// for each
document.querySelectorAll('#container a'), // <a> in #container
function (elm, idx, arr) { // get it's info (index etc.) then
elm.addEventListener('click', function() {console.log(idx);}, false);
// make clicking it log it's index
}
);
당 또는 <div>
Array.prototype.forEach.call(// for each
document.querySelectorAll('#container div'), // <div> in #container
function (elm, idx, arr) { // get it's info (index etc.) then
Array.prototype.forEach.call(// loop over each
elm.querySelectorAll('a'), // <a> in that <div>
function (a_elm, a_idx, a_arr) { // and
a_elm.addEventListener('click', function() {console.log(idx);}, false);
// make clicking it log the index of the <div>
}
);
}
);
주 당 많은 <a>
의가있는 경우 로그인 지수가 1에 대한 0
에서 시작이 있다면.
매력처럼 작동합니다. 감사합니다! –