我在 vex6.html 中創建了一個 iframe,在 styles.gq/test.js 中創建了一個 js 檔案,該檔案中有鏈接和 ID,但是當我嘗試在 html 中呼叫代碼時,它不起作用。
這是我的 vex6.html 代碼
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script type="text/javascript" src="https://styles.gq/test.js" ></script>
<iframe id="vex6" width="100%" height="500" style="border:1px solid black;"></iframe>
</body>
</html>
這是我的js代碼
document.getElementById("vex3").src
= "https://www.obviousplays.tk/gfiles/vex3";
document.getElementById("vex4").src
= "https://www.obviousplays.tk/gfiles/vex4";
document.getElementById("vex5").src
= "https://www.obviousplays.tk/gfiles/vex5";
document.getElementById("vex6").src
= "https://www.obviousplays.tk/Gfiles6/vex6";
document.getElementById("slope").src
= "https://www.obviousplays.tk/gfiles/slope";
我期待一個 iframe 但似乎沒有 iframe 的鏈接它也吐出錯誤 cannot set properties of null (setting(src))
uj5u.com熱心網友回復:
<script type="text/javascript" src="https://styles.gq/test.js</script>在 iframe 標記后添加該行
。
為我作業。
uj5u.com熱心網友回復:
控制臺錯誤:"script.js:3 Uncaught TypeError: Cannot set properties of null (setting 'src')"
您的腳本在 iframe 內容之前加載,這意味著您正在嘗試初始化未識別物件的源。
解決方案:將<script>標記放在您的 iframe 下方。<script>將標簽放在標簽的底部是一個很好的做法,<body>這樣我們就可以始終訪問加載的內容。然而,有時內容需要更多時間才能完全加載(盡管將<script>標簽放在頁面底部)。我建議將您的代碼包裝在window.onload = () {}其中,以確保在觸發代碼之前加載所有內容。
<body>
<iframe id="vex3" width="100%" height="500" style="border:1px solid black;"></iframe>
<script type="text/javascript" src="https://styles.gq/test.js"></script>
</body>
您的代碼很好,應該可以作業(請原諒批評)。任何時候你似乎使用重復代碼,這意味著它可以像這樣被簡化/自動化:
window.onload = () => {
// Target all <iframe> tags.
iframes = document.querySelectorAll('iframe');
// Loop through list of <iframe> nodes.
Array.from(iframes).map(iframe => {
// Update <iframe>'s attribute "src" to the origin URL,
// and use the iframe's attribute "id" value as the final source.
iframe.setAttribute('src', `https://www.obviousplays.tk/gfiles/${iframe.id}`)
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/534335.html
上一篇:加入兩個陣列并查找重復項
