1
의 버튼이 두 개 있지만 작동하지 않습니다. addEventListener
click
을 사용하려고합니다!addEventListener가 getElementsByClassName에 의해 다중 요소와 함께 작동하지 않습니다.
<div class="share-block">
<a href="#" class="btn-rounded share-btn"><i class="fa fa-share-alt fa-2x"></i></a>
<div class="social-block">
<a href="http://facebook.com/share" class="btn-rounded btn-sm social-btn btn-fb"><i class="fa fa-facebook"></i></a>
<a href="http://twitter.com/share" class="btn-rounded btn-sm social-btn btn-tw"><i class="fa fa-twitter"></i></a>
</div>
</div>
그리고 여기 javascript 코드입니다 :
이
은 html입니다var socialBtns = document.getElementsByClassName('social-btn');
for (var i = 0; i < socialBtns.length; i++) {
socialBtns[i].addEventListener('click', function(event) {
window.open(socialBtns[i].href, "Share", "width=600,height=400");
event.preventDefault();
});
}
대신에 루프에
socialBtns
변수를 참조하는 이벤트 핸들러에
가 http://jshint.com/ : 경고 :'내에서 기능을하지 마십시오 루프 '. –
@ DannyFardyJhonstonBermúdez 왜 내가 설명하면 안 될까요? –
관련 게시물이 있습니다. 이 링크를 참조하십시오 : https://stackoverflow.com/questions/3037598/how-to-fix-jslint-error-dont-make-functions-within-a-loop. –