好吧,我放棄了。
我正在嘗試制作這個腳本
<script>
$(document).ready(function(){
var d = new Date();
n = d.getMonth();
if (n == 11) {
src="extrafiles/effect/snow.js";
}
});
</script>
每當 12 月時呼叫“snow.js”。
雖然不會作業。想法?
編輯:
也許這是一個更好的方法?
if ( d.getMonth().match(11) ) { //呼叫snow.js; }
編輯2:
這適用于確定季節,可調整到月份
<script>
var currentTime = new Date();
var month = currentTime.getMonth() 1;
var total = month;
if (total == 12 || total == 1 || total == 2)
{
//beginning of script
START JS FILE VIA ???
//end of script
}
</script>
uj5u.com熱心網友回復:
您可以使用 document.write 添加腳本
if (new Date().getMonth()===11) document.write('<script src="https://ry3yr.github.io/OSTR/myhomepage/snow.js"><\/script>');
uj5u.com熱心網友回復:
嘗試這樣做:
if (n === 11) {
let myScript = document.createElement("script");
myScript.setAttribute("src", "https://www.example.com/snow.js");
document.body.appendChild(myScript);
makeSnow();
}
或在 ES6 模式下:
<!doctype html>
<script>
async function load() {
let say = await import('./snow.js');
say.makeSnow();
}
</script>
<button onclick="load()">Click me</button>
uj5u.com熱心網友回復:
您可以import根據自己的情況使用匯入檔案。您也可以匯入該檔案,只需在條件中從該檔案中呼叫您的函式即可。
<DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(async () => { // same as $(document).ready(async function(){
const month = new Date().getMonth()
if (month === 11) {
const effect = await import('./snow.js')
effect.run()
}
})
</script>
</head>
<body>
</body>
這是在開始時僅匯入檔案的示例
<DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
const effect = await import('./snow.js')
$(async () => {
const month = new Date().getMonth()
if (month === 11) {
effect.run()
}
})
</script>
</head>
<body>
</body>
請注意,這將在 Stack Overflow 上引發錯誤,因為該檔案不存在,但我已驗證它在本地作業。
既然你已經展示了這個檔案,我可以看到它實際上并沒有匯出任何東西,所以import在這里對你不起作用。相反,您將不得不追溯地將腳本添加到 DOM
<DOCTYPE html>
<head>
</head>
<body>
<script>
document.addEventListener('DOMContentLoaded', () => {
const s = document.createElement('script')
s.type = 'text/javascript'
s.src = 'https://ry3yr.github.io/OSTR/myhomepage/snow.js'
document.body.appendChild(s)
})
</script>
</body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381096.html
標籤:javascript 称呼
