我有一个将 iframe 写入页面的脚本。 我想定位此 iframe 内的链接以具有功能。 问题是: 编写 iframe 后,我使用此代码:
$('#iframe_pin').load(function () {
$(this).contents().find('a.close').click(function (event) {
event.preventDefault();
$('.loaded').fadeOut(fade_time).remove();
})
})
而且它不起作用。 但是如果我在加载函数之后放一个警告,像这样:
$('#iframe_pin').load(function () {alert('bla bla');
$(this).contents().find('a.close').click(function (event) {
event.preventDefault();
$('.loaded').fadeOut(fade_time).remove();
})
})
它有效!
当然我不能在代码中保留警报:)
谁能帮帮我?
最佳答案
从评论中你有类似的东西:
<div class="loaded">
<iframe id="iframe_pin" src="abc.html" width="100%" height="100%">
</div>
所以,您需要做的就是:
$("#iframe_pin").load(function() {
// let's get the iframe source
var iframe = $("#iframe_pin").contents();
iframe.find("a.close").bind("click", function() {
$(".loaded").fadeOut('slow', function() {
// now that the FadeOut is finished, let's remove
$(".loaded").remove();
});
return false; // let's prevent to jump in the click
});
});
working example at my own space
Example 2 by creating the
<iframe>
on the fly
Example 3 by creating the
<iframe>
on the fly, removing them and re-creating them
关于javascript - 加载 iframe 和目标内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263535/