我需要知道如何根据目录中有多少个html文件自动创建Iframes。假设我在main.html所在的子目录中有1.html和2.html。我想自动创建2个框架(在main.html中),一个显示1.html,另一个显示2.html。顺便说一句,如果你看不出来,2.html和1.html是本地文件
发布于 2016-03-03 14:40:04
试着像这样实现:
$.ajax({
url: "/images-folder-on-server/",
success: function (data) {
var file_count = $(data).length();
$.each( data, function( key, value ) {
appendIframe(value);
});
}
});
function appendIframe( file)
{
var iframe = document.createElement('iframe');
iframe.src = file;
iframe.style.width = "100px";
iframe.style.height = "100px";
document.body.appendChild(iframe);
}发布于 2016-03-03 14:33:40
向服务器发出ajax调用,以获取目录中的文件列表
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(){
//iterate the list and call addIframe( src ) for each of them
};
httpRequest.open('GET', "listFilesInDirectory");
httpRequest.send();这就是addIframe的样子
function addIframe( src )
{
var iframe = document.createElement('iframe');
iframe.src = src;
iframe.style.width = "640px";
iframe.style.height = "px";
document.body.appendChild(iframe);
}
addIframe( "subdir/1.html" );
addIframe( "subdir/2.html" );发布于 2016-03-03 14:49:38
我认为问题不在于如何使用Javascript创建iFrame,而在于如何计算使用它的文件数量。我建议创建对服务器端语言的ajax请求,以计算文件的数量,然后您可以使用该数量通过jQuery创建iFrame元素。您可以使用PHP编写的以下代码作为服务器端脚本:
<?php
function getNoFiles(){
$i = 0;
$dir = 'yourHTMLDirectory/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
echo $i;
}
?>您可以使用这样的jQuery简单代码在简单循环下创建,并限制从PHP代码中获取的计数。
$('<your iframe tag></iframe>').appendTo('your div or area class or id');https://stackoverflow.com/questions/35764713
复制相似问题