按钮事件在第一次点击后丢失

huangapple go评论62阅读模式
英文:

Button event lost after first click

问题

子HTML页面中的JavaScript代码存在问题。在事件监听器函数的结尾缺少一个闭合的括号。以下是修复后的代码:

var plan0 = '<div class="popup">'+
            '<span id="closeMe">×</span>'+
            '其他内容'+
            '</div>';
document.getElementById("open0").addEventListener("click", function(event) {
    event.preventDefault();
    parent.showPopup(plan0);
}); // 此行是修复的地方

在上面的代码中,我添加了缺失的闭合括号,这样你的事件监听器就会正常工作了。

英文:

I have a parent HTML page. It embeds an iframe containing a child HTML page.

The child HTML stores a block of HTML, including a "close" button, into a JS variable, and pass this variable to parent JS function upon a click of a "Show" button in the child HTML. This "Show" button has an addEventListener() on click. The goal of this button is to display the block of HTML code on top of the parent HTML.

I managed to make this block of HTML display on top of the parent HTML when I click "Show", the close button in this block will "display: none" upon clicking the cross button.

However, any further click of the "Show" button in the child HTML will not work; the button is broken and has no action.

Child HTML:

var plan0 = '<div class="popup">'+
            '<span id="closeMe">×</span>'+
            'other content here'+
            '</div>';
document.getElementById("open0").addEventListener("click", function(event) {
    event.preventDefault();
    parent.showPopup(plan0);
}

Parent HTML:

 <div>
      <iframe id="channelFrame" scrolling="no" style="width: 100%; border: 1px; overflow: hidden" src="child.html"></iframe>
 </div>

 <div id="popup"></div>

 <script>
       function showPopup(info) {
              var popup = document.getElementById("popup");
              popup.innerHTML = info;
              var closeMe = document.getElementById("closeMe");
              if (closeMe) {
                     closeMe.onclick = function() {
                              popup.style.display = "none";
                              popup.innerHTML = "";
                      }
               }
        }
  </script>

There is no error in console, and the "event" is still clearly marked in the "Show" button.

Where is the error?

答案1

得分: 1

On the first click on close, you are hiding the popup with:

popup.style.display = "none";

but never un-hide it. Any subsequent actions will happen in a hidden tag, so you don't see anything.

Try something like:

function showPopup(info) {
    var popup = document.getElementById("popup");
    popup.innerHTML = info;
    popup.style.display = "block";          // <------ show it again
    var closeMe = document.getElementById("closeMe");
    if (closeMe) {
        closeMe.onclick = function() {
            popup.style.display = "none";
            popup.innerHTML = "";
        }
    }
}
英文:

On the first click on close, you are hiding the popup with

popup.style.display = &quot;none&quot;;

but never un-hide it. Any subsequent actions will happen in a hidden tag, so you don't see anything.

Try something like:

   function showPopup(info) {
          var popup = document.getElementById(&quot;popup&quot;);
          popup.innerHTML = info;
          popup.style.display = &quot;block&quot;;          // &lt;------ show it again
          var closeMe = document.getElementById(&quot;closeMe&quot;);
          if (closeMe) {
                 closeMe.onclick = function() {
                          popup.style.display = &quot;none&quot;;
                          popup.innerHTML = &quot;&quot;;
                  }
           }
    }

huangapple
  • 本文由 发表于 2023年2月20日 00:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定