怎么能循环、顺序地从iframe中提取信息?
背景:我想在父页面中创建iframe,以从另外其它10个不同页面中不同信息。
我想到两种方法,都没走通:
1.创建一个iframe,然后循环修改iframe.src的值,但不知道为什么每次修改src都会刷新父页面?我只希望iframe内部刷新,而不刷新父页面,怎么做?
iframe.onload = function() {
iframe.setAttribute("src", someUrl);
}2.每次循环先创建一个iframe,然后把数据传给父页面,最后删掉iframe。这样的问题是传数据不是顺序进行的,我希望第一个页面的数据传完后删掉iframe,然后再创建第二个页面的iframe再传数据。而现在10个页面的传输数据我无法用for循环控制,怎么能强制一个iframe传完数据后再进行下一个循环呢?
for(var userIndex=1; userIndex<10; userIndex++) {
iframe.onload = function() {
iframe.setAttribute("src", document.getElementsByClassName("photo_wrap")[userIndex].getElementsByTagName("a")[1].getAttribute("href"));
}
chrome.extension.onMessage.addListener(function(data) {
var newHeader = document.createElement("h4");
txt = document.createTextNode(data);
newHeader.appendChild(txt);
content.appendChild(newHeader);
});
iframe.onload = function() {
iframe.parentNode.removeChild(iframe);
}[解决办法]
不会啊,怎么会刷新父页面,不要iframe.parentNode.removeChild(iframe)这样删除iframe,你直接改变iframe的Src就好,
$("iframe的ID").attr("src",someUrl);基于事件就要你自己加判断了
[解决办法]
在onload中控制
var nowIndex=1;
window.onload = function () {
var ifr = document.getElementById('iframe的ID');
ifr.src = '第一个url';
ifr.onload = function () {
nowIndex++;//
if (nowIndex < 10) {
ifr.src = ''; //根据nowIndex配置不同的url
}
}
}