js顯示多張圖片的問題

來源:互聯網
上載者:User

閑來無事,自己敲了段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)";//Firefox上不能這樣寫,應寫成下面這種形式
//inpu.setAttribute("onchange","showPic(this)");
*/
}
function showPic(t){
   var imgl=document.createElement("img");
alert(t.value);
imgl.src=t.value;//IE上顯示全路徑而FF為了安全只顯示檔案名稱,如果圖片與該網頁同路徑可以顯示但是在其他地方就不行了

/**

下面是網上找的Firefox顯示檔案全路徑的方法,可我試了不行。。。

//判斷瀏覽器類型
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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.