iT邦幫忙

5

$document ready 和 $funcion 的迷思?要用哪個才對?以及使用的時間點?

  • 分享至 

  • xImage
$(document).ready(function() {

});
$(window).ready(function () {

});
$(window).load(function() {

});
$(window).on('load', function () {
     
});
$(function() {

});
(function($) {

})(jQuery);
jQuery(document).ready(function ($) {

});

想問一下要用哪個才對?還是有其他更好的?
以及使用的時間點?

另外我發現我把這個拔下來,js代碼還是可以運行...所以是要在什麼樣的情況下才需要使用?
再來就是看哪個最通用?

圖片
  熱門推薦
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
8
最佳解答

用這個最好,因為最精簡,其他可以忽略,使用的時間點在於確保html全部load完,
才開始執行js,避免js找不到html dom而發生錯誤,這種寫法等同把js寫在html之後

$(function() {
});
看更多先前的回應...收起先前的回應...
火爆浪子 iT邦研究生 1 級 ‧ 2018-08-16 10:25:30 檢舉

假設我js檔案很多個,每一個都要 $(function(){ 嗎?

是的,每個js檔案都是獨立,如果兩個js要使用同一個變數值,就把變數宣告成全域的,就這樣

fillano iT邦超人 1 級 ‧ 2018-08-16 14:47:17 檢舉

我記得放在</body>之前,其他dom元素之後就可以。根元素就是<html>,放在他之後有點怪,雖然也不會有問題啦。

可以放在之前,是因為dom都已載入了,而且tag只要載入開頭的tag,就算有載入成功,所才才會跟放在html外,使用上沒什麼區別吧

舜~ iT邦高手 1 級 ‧ 2018-08-16 15:36:07 檢舉

有一個前提,在直行該段程式碼,必須先載入jQuery,不然該段會因出錯而被忽略

火爆浪子 iT邦研究生 1 級 ‧ 2018-08-16 23:06:14 檢舉

我發現有些東西沒辦法這樣做...
好比我有一個檔案是放js function
like...


function (xxxxx){
    xxxxxx
}

我發現放了那個就會失效

function 也算是一個變數,如果要全域用,妳知道的,不知道就…/images/emoticon/emoticon05.gif

1
舜~
iT邦高手 1 級 ‧ 2018-08-16 15:39:53

還有一種方式,假設不透過jQuery的方法,或不確定jQuery是否載入
除了IE8外,其他瀏覽器都支持

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});

參考資料
https://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery

0
jacobhue
iT邦見習生 ‧ 2022-01-22 14:27:59

這是許多開發人員常犯的錯誤。 他們使用 $document ready 而不是 $funcion? 因為它寫起來更短,並且適用於所有情況。

描述:事實是! 在 $func 的末尾? 不允許連續調用多個異步函數。 為了連續調用多個異步函數,我們需要使用 jQuery 1.7 中引入的 .waitUntil 方法

那麼,我們如何知道我們的代碼什麼時候會異步執行,什麼時候不會呢? 例如,如果您使用 $(document).ready() 之類的東西,那麼您的代碼總是同步執行,因為不涉及等待。

0

@geometry dash online 会是你的IP吗?

0
andreen
iT邦見習生 ‧ 2024-04-22 15:48:59

同步代碼:當代碼按照編寫的順序一行一行執行,並且在執行完畢之前不會暫停或等待任何其他操作時,稱為同步代碼。例如,使用 $(document).ready() 會導致代碼在文檔完全加載後才執行,這是同步的。
異步代碼:當代碼不按照順序執行,而是在某些事件發生或滿足特定條件時執行,drift hunters 並且可能會等待其他操作完成後才執行,稱為異步代碼。例如,使用 $.ajax() 函數發送 AJAX 請求時,代碼將等待服務器響應之前不會阻塞,這是異步的。

0
vidmateapps
iT邦見習生 ‧ 2024-10-17 00:41:18

VidMate APK is a Free Video Downloader App for Mobile Phones. Download VidMate and Get Free MP3 Music, HD videos from Facebook, Whatsapp & TikTok etc. https://vidmateapps.org/

0
03233438728
iT邦見習生 ‧ 2024-10-17 13:15:03

SnapTube App is a Free Video Downloader for Android.

0
03197240470
iT邦見習生 ‧ 2024-10-26 17:04:06

Insta Pro APK is a modified version of Instagram for Android devices. Instagram Pro allows Users to Download Posts, Stories, Reals and IGTV.
https://instaproapkdownload.net/

0
honista
iT邦見習生 ‧ 2024-10-27 14:38:48

Honista is an Instagram Mod Developed by Honista Team. Honista APK Provides Many Features such as Themes, Download Media, Hide Visibility, and Better Privacy. https://honista.cc/

0
usmankhan
iT邦見習生 ‧ 2024-11-03 23:08:54

Download the latest version of GBWhatsApp APK Official (Anti Ban). GBWhatsApp APK Download is a popular WhatsApp MOD and an alternative. https://gbplus.app/

0

CapCut Pro Mod APK removes the limits discovered in the standard version of the application. It unlocks premium features, giving users what they were restricted to experience in the normal version of the application.

我要發表回答

立即登入回答