1 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
您在這里重復(fù)了很多事情,導(dǎo)致它沒(méi)有得到優(yōu)化。如果你會(huì)使用函數(shù),請(qǐng)使用它們!如果沒(méi)有,更好的方法是:
function populateTable() {
? for (var i = 0; i < data.length; i++) {
? ? // Look how I have made the file addition here.
? ? var row = '<tr><td><a href="' + (data[i].type == "MOV" ? "video" : "image") + ".html?=" + data[i].file + '">' + data[i].title + "</a></td>";
? ? row += "<td>" + data[i].year + "</td></tr>";
? ? // Do the MOV vs. PNG thing/
? ? row += "<tr><td>";
? ? if (data[i].type == "MOV") {
? ? ? row += `<div class="container">
? ? ? ? ? ? <video autoplay muted loop id="main">
? ? ? ? ? ? ? <source src="${data[i].file}" type="video/mp4" />
? ? ? ? ? ? ? Your browser does not support HTML5 video.
? ? ? ? ? ? </video>
? ? ? ? </div>`;
? ? } else {
? ? ? row += `<div class="container">
? ? ? ? ? ? <img src="${data[i].file}" alt="${data[i].title}" />
? ? ? ? </div>`;
? ? }
? ? row += "</td></tr>";
? ? $("#contents").append(row);
? }
}
在上面的代碼中:
看看我是如何在第 4 行添加文件的。
在第 7 行執(zhí)行 MOV 與 PNG 的操作。
對(duì)于模板化的事情,使用How to read GET data from a URL using JavaScript??,你可以做的是:
<html>
? ? <body>
? ? ? ? <div class="container">
? ? ? ? ? ? <video autoplay muted loop id="main">
? ? ? ? ? ? ? <source src="" id="src" type="video/mp4" />
? ? ? ? ? ? ? Your browser does not support HTML5 video.
? ? ? ? ? ? </video>
? ? ? ? </div>
? ? </body>
? ? <script>
? ? ? ? var params = new URLSearchParams(location.search);
? ? ? ? document.getElementById("src").setAttribute("src", params.get('file'));
? ? </script>
</html>
添加回答
舉報(bào)