在If-else块中使用ready()方法来调用函数是一个好习惯吗?

问题描述 投票:0回答:1

我正在尝试在加载时调用 test() 函数。我就是这样做的:

if ($('.partical .sectionA').length) {
    $(document).ready(function(){
        test($(this));
    });
 }
test(something){...}

我发现在 If Else 语句中使用

ready()
方法很奇怪,我想知道这是否是一个好的做法,或者是否有更好的方法在页面加载时调用测试函数?

javascript jquery
1个回答
0
投票

这是错误的做法。在标签工作之前它必须是内联的。那么准备时的

$(this)
是什么呢?

也许你的意思是

$(function(){
  const $sectionA = $('.partical .sectionA');
  if ($sectionA.length) test($sectionA);
});
© www.soinside.com 2019 - 2024. All rights reserved.