다음 코드를 실행할 때 드롭 다운 메뉴의 Current Children 옵션을 클릭하면 해당 모달이 화면에 표시되지만 나는`Uncaught TypeError로 인해 모달을 닫을 수 없습니다 : null의 'onclick'속성을 설정할 수 없습니다.
spanModal.onclick = function()
오류에 다음과 같은 오류로 닫을 수없는 생각입니다
userTemplate.js:25 Uncaught TypeError: Cannot set property 'onclick' of null
at HTMLAnchorElement.<anonymous> (userTemplate.js:25)
at HTMLAnchorElement.dispatch (jquery-3.2.1.js:5206)
at HTMLAnchorElement.elemData.handle (jquery-3.2.1.js:5014)
가 왜이 오류가 무엇입니까?
HTML :
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Children <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#" class="menu" id="currentChildren">Current Children</a></li>
<li>
</ul>
</li>
</ul>
</div>
<div id="currentChildrenModal" class="modal">
<div class="modal-content">
<span class="close" id="currentChildrenModalSpan">×</span>
<p>List of all currently enrolled children. Include option to filter by group.</p>
</div>
</div>
자바 스크립트 :
$(".menu").on("click", function()
{
var modalFireButton;
var modalName;
var spanModal;
var span;
//get Id of button cliced
modalFireButton = $(this).attr('id');
//set variable for corresponding modal
modalName = (modalFireButton + "Modal");
modalName = document.getElementById(modalName);
span = (modalName + "Span");
spanModal = document.getElementById(span)
// spanModal = document.getElementById(spanModal);
modalName.style.display='block';
spanModal.onclick = function()
{
modalName.style.display = "none";
}
});
있다는 걸쳐 처리 얻을
modalName.id
를 사용Span
를 추가 할 필요가<div id="currentChildrenModal" class="modal">
에와currentChildrenModal
을 얻는 의미 옳은. 귀하의 경우, 당신은 span name이 "modal.id +"Span " –