微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为使用 document.write 创建的弹出窗口添加背景图像

如何解决为使用 document.write 创建的弹出窗口添加背景图像

我有一些 JavaScript 代码,可以在单击图像时向页面添加“弹出”窗口。我正在尝试使用“点击”图像作为弹出窗口的背景图像。

我能够创建弹出窗口,但是传递要用作背景的路线证明是有问题的。这是我的代码概要:

//Get an array of photo images from the page
var _photos = document.querySelectorAll(".gallery_pix");

// Loop through the resulting array
for(var i = 0; i < _photos.length; i++){
_photos[i].addEventListener("click",function() {

var _img_select = this.src;
var _img_id = this.id;

var _splits = _img_id.search("_");  //return the position of the FirsT "underscore ('_')
var _pix_name = _img_id.substring(_splits + 1);  
  //return the 'name' of the clicked image

//Create a 'popup' window...
var generator=window.open('','name','height=400,width=500');

generator.document.write('<html><head><title>Popup</title>');
generator.document.write('<link rel="stylesheet" href="style.css">');

generator.document.write('</head><body>');
generator.document.write("<div>");
generator.document.write("<h1>Out with the old,in with the new!</h1>");
generator.document.write("</div>");
generator.document.write('</body></html>');
generator.document.close();

var sheet = generator.document.createElement('style')
sheet.innerHTML = "div {border: 2px solid black; background-image: _img_select; background-color: blue;}";
generator.document.body.appendChild(sheet);

本质上,问题在于包含“点击”图像(“_img_select”)的完整路径的变量不能作为弹出窗口“body”标签的“background-image”源传递。我该如何纠正?我提前谢谢你。问候。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。