英文:
Button event lost after first click
问题
子HTML页面中的JavaScript代码存在问题。在事件监听器函数的结尾缺少一个闭合的括号。以下是修复后的代码:
var plan0 = '<div class="popup">'+
'<span id="closeMe">&times;</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">&times;</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 = "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 = "";
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论