我有一個檔案夾,里面myfolder有幾個檔案。我想只選擇這個檔案夾的 HTML 檔案,并將它們隨機放入 iframe。
所以這是我的想法:
// Get HTML files
var pages = ["myfolder/1.html", "myfolder/2.html", "myfolder/3.html"]; // Instead of this, all HTML files of the folder 'myfolder/' should be selected
// Get one random HTML file of selection and add it to the iframe
$(".random-page").attr("src", "myfolder/" pages[Math.floor(Math.random() * pages.length)]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<iframe class="random-page"></iframe>
可以寫什么來代替var pages = ["myfolder/1.html", "myfolder/2.html", "myfolder/3.html"];從一個檔案夾中選擇所有 HTML 檔案?那么它是否適用于我撰寫的代碼?
uj5u.com熱心網友回復:
如果您知道將有 10 個檔案,名為1.html,2.html等,則不需要您的pages陣列:
$(".random-page").attr('src', 'myfolder/' ( Math.floor(Math.random() * 10) 1 ) '.html');
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/503676.html
標籤:jQuery
