另一个新功能中的新功能
New function in another new function
问题
已解决: (http://jsfiddle.net/336e6pbh/15/) (解决方案).
我有这个简单的js代码:jsfiddle(jsFiddle链接)。
Desktop();
GalleryButton();
function Desktop() {
var wrapper = document.getElementById("wrapper");
var footer = document.createElement("footer");
var content = document.createElement("div");
wrapper.appendChild(content);
content.appendChild(footer);
footer.id = "mainFooter";
content.id = "content";
}
function GalleryButton() {
var galleryImg = document.createElement("img");
var galleryButton = document.createElement("a");
galleryButton.setAttribute("href", "#");
galleryImg.setAttribute("src", "pics/gallery.png");
var bottom = document.getElementById("mainFooter");
galleryButton.appendChild(galleryImg);
bottom.appendChild(galleryButton);
galleryButton.onclick = function () {
new NewBox();
};
}
function NewWindow() {
var popup = document.createElement("div");
var content = document.getElementById("content");
popup.id = "popup";
content.appendChild(popup);
}
function NewBox() {
new NewWindow();
var box = document.createElement("div");
var content = document.getElementById("popup");
box.id = "box";
popup.appendChild(box);
}
当我单击左上角的 img 时,会出现一个新的弹出窗口,里面有一个黄色框。我的问题是如何在新弹出窗口中获得带有新黄色框的新弹出窗口?
我可以将代码从 NewWindow 放到 NewBox 函数,但我希望这些函数分开。
你对document.getElementById("popup")的第二次调用是找到第一个带有id=popup的div,并将黄色框放在那里。
您的元素 ID 应该是唯一的:HTML 规范
检查这个jsfiddle,我已经更改了您的代码和CSS
http://jsfiddle.net/336e6pbh/5/
function GalleryButton() {
var galleryImg = document.createElement("img");
var galleryButton = document.createElement("a");
galleryButton.setAttribute("href", "#");
galleryImg.setAttribute("src", "pics/gallery.png");
var bottom = document.getElementById("mainFooter");
galleryButton.appendChild(galleryImg);
bottom.appendChild(galleryButton);
galleryButton.onclick = function () {
new newPopUp();
};
}
function newPopUp(){
var content = document.getElementById("content");
var popup = document.createElement("div");
popup.className = "popup";
var blueBox = document.createElement("div");
blueBox.className = "blueBox";
popup.appendChild(blueBox);
content.appendChild(popup);
}
你的CSS会是这样的
#content {
background-color: #ccc;
height:300px;
width:350px;
}
#mainFooter {
border:1px solid #000;
height:50px;
background-color: rgba(0, 0, 0, 0.4);
}
.popup {
background-color: blue;
border:1px solid pink;
height:100px;
width:150px;
}
.blueBox {
background-color: yellow;
height:20px;
width:40px;
}
您的 HTML 代码将保持原样。
相关文章:
- 将原型调用为新对象中的另一个原型
- 具有双重功能的按钮:在_blank窗口中打开一个链接,再加上_self中的另一个链接
- 使用单选按钮参考另一个功能
- 一个Ajax函数能产生另一个Ajax功能吗
- 通过模糊功能传递链接,而不是单击另一个链接
- 从Backbone中的另一个视图调用新视图
- 如何从javascript散点图中的另一个变量为x和y添加新值
- 根据另一个字段选择获取新值后更新下拉列表内容
- EXTJS 4.2:点击按钮,在新窗口中将选定的网格行从一个网格复制到另一个网格
- Jquery使用Slice将前N个元素包装成一个新元素,并将其余N个元素打包成另一个新元素
- 如果另一个功能完成,则执行该功能,而不是何时
- 如果选中单选按钮,如何转到另一个功能?(Javascript)
- 将图像数组从一个窗口传递到另一个窗口,并在javascript的新窗口中显示图像
- 当现有 Google 标记移动到另一个位置时,如何更新新的纬度和经度
- 实现另一个要“onSongEnd”运行的功能
- 如何将一个数组从 javascript 文件写入另一个新的 js 文件
- 加载另一个相同页面时,使用新数据激活第一个网页
- 如何在鼠标悬停在另一个图像/链接上时显示新图像/窗口
- 从一个功能到另一个功能的呼叫转移
- 另一个新功能中的新功能