[英]jQuery remove twice function call
請考慮這個非常簡化的示例。
當屏幕尺寸較小時,下面的代碼從頁面上刪除固定的導航欄。 它名為removeFixedNavbar()
。
加載文檔和調整窗口大小時應執行此操作。 請查看代碼。 好吧,這段代碼看起來有些丑陋(兩次調用removeFixedNavbar()
,這是一種更好的編寫方式(使用更少的代碼))。
$(document).ready(function() {
removeFixedNav();
$(window).resize(function(){
removeFixedNav();
});
});
$(document).ready(removeFixedNav);
$(window).resize(removeFixedNav);
這應該工作,但是我認為僅使用CSS Media Queries
(取決於用例)才應該可行。
更簡單的可能是:
$(function(){
$(window).resize(removeFixedNav).trigger("resize");
});
將其放在最底部可以擺脫$(function(){...})
其他等效版本可以是:
$(function(){
$(window).trigger("resize");
});
$(window).resize(removeFixedNav);
好吧,您在document.ready中兩次調用它。 放置窗口。 調整外部document.ready事件的大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.