我有一个像这样的onclick事件:
function OnVisClicked() {
$("#overlay").animate({opacity: "toggle",width: "3000px",height: "3000px"}, 300);
$('<iframe />', {
name: 'myFrame',
id: 'mytFrame',
width: 724,
height: 535,
frameborder:"0",
scrolling: "no",
allowTransparency: "true",
src: "the link..."
}).appendTo('.myDiv').ready(function(){
$(".myBigDiv").show();
});
$("#someOtherDiv").css("display", "none");
}
原来myBigDiv
设置为display:none
。
点击我的按钮后,myFrame
已成功附加到myDiv
。但myBigDiv
仍然是display:none
。我在ready()
内添加了一个断点,但很明显程序没有进入它。
我得到答案和解决方案后的附加信息:
由于页面上的其他网页部分,我的DOM无法完全加载,因此永远无法调用ready()
。
答案 0 :(得分:2)
只有在文档准备好后才会调用ready()函数http://api.jquery.com/ready/ 我想你正在寻找像onload()那样的其他东西
function OnVisClicked() {
$("#overlay").animate({opacity: "toggle",width: "3000px",height: "3000px"}, 300);
$('<iframe />', {
name: 'myFrame',
id: 'mytFrame',
width: 724,
height: 535,
frameborder:"0",
scrolling: "no",
allowTransparency: "true",
src: "the link..."
}).appendTo('.myDiv');
document.getElementById("mytFrame").onload = function() {
$(".myBigDiv").show();
}
}
答案 1 :(得分:0)
.ready()方法通常与属性不兼容。如果必须使用load,则不要使用.ready()或使用jQuery的.load()方法将加载事件处理程序附加到窗口或更具体的项目,如图像。
.ready( handler )
- 在DOM准备好后执行的函数。
如果您想在页面加载上附加iframe,请尝试下面的代码。
$(document).ready(function() {
$('<iframe />', {
name: 'myFrame',
id: 'mytFrame',
width: 724,
height: 535,
frameborder:"0",
scrolling: "no",
allowTransparency: "true",
src: "the link..."
}).appendTo('.myDiv');
});
如果我理解你的问题,这对你有帮助。如果您有任何问题,请告诉我。
答案 2 :(得分:0)