js显示多张图片的问题

来源:互联网 发布:网络用语ip是什么意思 编辑:程序博客网 时间:2024/06/11 06:36

闲来无事,自己敲了段js,发现诸多问题,暂不得其解:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
 <head>
  <title> New Document </title>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
//或者在body里加个onload方法
window.onload=function(){
var num=prompt("您打算显示多少张图片","")-0;
for(var i=1;i<num+1;i++){
var im=document.createElement("img");
//怎么能让src可以匹配所有图片而不是固定的“i.gif”?
im.src=i+".gif";
   //document.body.appendChild(im);
document.getElementsByTagName("body")[0].insertBefore(im,document.getElementById("in"));
if((i%5)==0){
var br=document.createElement("br");
document.body.insertBefore(br,document.getElementById("in"));
}
}
/**
var inpu=document.createElement("input");
inpu.type="file";
document.body.appendChild(inpu);
inpu.onchange="showPic(this)";//火狐上不能这样写,应写成下面这种形式
//inpu.setAttribute("onchange","showPic(this)");
*/
}
function showPic(t){
   var imgl=document.createElement("img");
alert(t.value);
imgl.src=t.value;//IE上显示全路径而FF为了安全只显示文件名,如果图片与该网页同路径可以显示但是在其他地方就不行了

/**

下面是网上找的火狐显示文件全路径的方法,可我试了不行。。。

//判断浏览器类型
var isIE=(document.all)?true:false;
var isIE7=isIE&&(navigator.userAgent.indexOf('MSIE 7.0')!=-1);
var isIE8=isIE&&(navigator.userAgent.indexOf('MSIE 8.0')!=-1);
if(isIE7||isIE8){
t.select();
var path=document.selection.creatRange().text;
document.selection.empty();
imgl.src=path;
}
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}catch(e){
alert("'由于浏览器安全问题 请按照以下设置 [1] 地址栏输入 "about:config" ; [2] 右键 新建 -> 布尔值 ; [3] 输入 "signed.applets.codebase_principal_support" (忽略引号).");
//return;
}
var fname=t.value;
var file=Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
try{
file.initWithPath(fname.replace(/\//g,"\\\\"));
}catch(e){
if(e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;
alert('无法加载文件');
return;
}
//imgl.src=file.path;
imgl.src=t.value;*/
document.body.insertBefore(imgl,document.getElementById("in"));
//document.body.appendChild(imgl);
}
</script>
 </head>
 <body >
  <input id="in" type="file" onchange="showPic(this)">
 </body>
</html>
原创粉丝点击