Adding div element to body or document in JavaScript

后端 未结 9 2073
醉梦人生
醉梦人生 2020-11-27 14:14

I am creating a light box in pure JavaScript. For that I am making an overlay. I want to add this overlay to body but I also want to keep the content on the page. My current

相关标签:
9条回答
  • 2020-11-27 14:38

    Try this out:-

    http://jsfiddle.net/adiioo7/vmfbA/

    Use

    document.body.innerHTML += '<div style="position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000;"></div>';
    

    instead of

    document.body.innerHTML = '<div style="position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000;"></div>';
    

    Edit:- Ideally you should use body.appendChild method instead of changing the innerHTML

    var elem = document.createElement('div');
    elem.style.cssText = 'position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000';
    document.body.appendChild(elem);
    
    0 讨论(0)
  • 2020-11-27 14:44

    Try doing

    document.body.innerHTML += '<div style="position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000;"></div>'
    
    0 讨论(0)
  • 2020-11-27 14:47

    Instead of replacing everything with innerHTML try:

    document.body.appendChild(myExtraNode);

    0 讨论(0)
提交回复
热议问题