简体   繁体   English

CreatePopup替换

[英]CreatePopup replacement

Using regular JavaScript or JQuery I want to replace the createPopup() method like done in this post: 使用普通的JavaScript或JQuery,我想像在这篇文章中那样替换createPopup()方法:

A universal createPopup() replacement? 通用createPopup()替代吗?

but I want to use a Div instead of an iFrame. 但我想使用Div而不是iFrame。 I don't need anything fancy just a simple div which can be styled. 我不需要花哨的只是可以设置样式的简单div。

The problem with using a Div is that I have a lot of existing code like this which I would like to remain untouched eg 使用Div的问题是我有很多这样的现有代码,例如,我希望保持不变。

var popup = window.createPopup();
oPopup.document.body.innerHTML = "Click outside <strong>popup</strong> to close.";

In the new createPopup() method below, is there a way to return an object that has the properties document.body.innerHTML to style the Div and the existing code can remain untouched. 在下面的新createPopup()方法中,有一种方法可以返回一个具有document.body.innerHTML属性的对象来设置Div的样式,并且现有代码可以保持不变。

if(!window.createPopup){
  window.createPopup = function() {

    // TODO return div object

    }
}

You can use javascript setters and getters in combination with defineProperties to pull off what you are trying to do. 您可以将javascript setter和getter与defineProperties结合使用,以完成您要尝试的操作。

 if(!window.createPopup){ window.createPopup = (function() { // build our object var o = { document: { body: { _innerHTML: '' } } }; // build the popup o.document.body._outer = document.createElement('div'); o.document.body._inner = document.createElement('div'); o.document.body._outer.className = 'modal'; o.document.body._inner.className = 'inner'; // attach popup o.document.body._outer.appendChild(o.document.body._inner); document.body.appendChild(o.document.body._outer); // add a property for innerHTML Object.defineProperties(o.document.body, { 'innerHTML': { get: function () { return this._innerHTML; }, set: function (x) { this._innerHTML = x; this._inner.innerHTML = this._innerHTML; } } }); // return the object return o; }); } var oPopup = window.createPopup(); oPopup.document.body.innerHTML = "Click outside <strong>popup</strong> to close."; 
 .modal { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0,0,0,0.5); } .modal .inner { padding: 2em; background: #fff; border: 1px solid #eee; display: inline-block; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM