當我運行下面的代碼時,當我點擊下拉菜單中的當前子項選項時,相應的模式顯示在我的屏幕上,但隨後我我無法關閉它,因爲我在'由於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>
的Javascript:
$(".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";
}
});
這正確。在你的情況,似乎你想跨度名稱是像「modal.id +」跨度「 –