javascript - 单击时删除附加的子项

标签 javascript html css

我正在使用以下内容在网站加载时创建图像弹出窗口 -

<script type="text/javascript">     
function showPopup()
{
 var div = document.createElement('div');
 div.className += 'popup';
 div.innerHTML = "<img src='startbutton.png' width='400' height='293' >"
 document.body.appendChild(div);
}

window.onload =  showPopup;

这是CSS

<style type="text/css">

.popup{
    position:absolute;
    width: 0px;
    height: 0px;
    left:40%;
    top:30%;
}
  1. 我如何修改它以便在点击时图像消失?
  2. 是否可以让页面的其余部分“淡出”直到图片被点击?

类似于模态对话框。

最佳答案

function showPopup() {
    var div   = document.createElement('div');
    var cover = document.createElement('div');
    var image = document.createElement('img');

    image.src    = 'startbutton.png';
    image.width  = 400;
    image.height = 293;

    cover.style.position   = 'fixed';
    cover.style.background = 'rgba(0,0,0,0.5)';
    cover.style.height     = '100%';
    cover.style.width      = '100%';
    cover.style.top        = '0';
    cover.style.left       = '0';

    div.className      = 'popup';
    div.style.position = 'fixed';
    div.style.top      = '50%';
    div.style.left     = '50%';
    div.style.margin   = '-200px 0 0 -146px';

    div.appendChild(image);

    image.onclick = function() {
        div.parentNode.removeChild(div);
        cover.parentNode.removeChild(cover);
    }

    document.body.appendChild(cover);
    document.body.appendChild(div);
}

window.onload =  showPopup;

FIDDLE

关于javascript - 单击时删除附加的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21003893/

相关文章:

javascript - 通过单击将 float div 添加到表格行

javascript - Bootstrap 3 导航栏 header 在移动设备上不可点击/响应

php - 创建 child 主题。 "Warning: file_get_contents9():failed to open stream: No such file or directory"错误

javascript - Google Maps API 3 - 根据缩放级别显示/隐藏标记

javascript - triple des 加密 c# 并在 javascript 中解密

javascript - 使用 Vue 中的 useVirtualList 来渲染 HeadlessUI 的 Listbox 组件

javascript - slidingDown() 相互滑动时如何防止图像闪烁

html - 如何防止 float 的子 div 换行?

javascript - iPad Web 应用程序 - 主屏幕图标未更新

javascript - 移动版 Safari 浏览器的滚动问题