[英]Uncaught Reference Error when trying to pass a function through a button in JavaScript?
[英]Getting Uncaught Reference Error when changing the function of a button
當元素具有子節點時,我試圖獲取一個按鈕來顯示模式。 因此,當您單擊它時,它會激活該功能並相應地顯示模態,它會通過將 id“myBtn”添加到button
元素來實現此目的。
這是我的 HTML:
<li id='parentNode'>
<button onclick="child_check()" id="button" class='customer-btn'><a href='#'>Customer 6</a></button>
<ul>
<li id='childNode'>
<a href='#'>Customer A</a>
</li>
</ul>
</li>
這是我的 JS:
<script>
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById(" myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
// Check to see if the list element has a child
function child_check() {
var child = document.getElementById("childNode");
var parent_check = document.getElementById("parentNode").contains(child);
var parent = document.getElementById("parentNode")
var button = document.getElementById("button")
if (parent_check){
button.id += " myBtn";
// Code that activates modal
// Get the modal
// When the user clicks on the button, open the modal
}
}
</script>
似乎工作正常,仍然收到以下錯誤:
未捕獲的類型錯誤:無法在 child_check 處讀取 null 的屬性“包含”(配置文件:259)
除了模態(我在這里看不到模態代碼),您可能可以像這樣在+= " myBtn"
行中添加一個空格,以成功為按鈕提供一個 ID 為“myBtn”。 如果沒有,只需使用簡單的“=”並為其提供所需的所有 ID,並用空格分隔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.