jQuery链中动态命名的函数调用[重复]。

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

我有一个类似的问题 本栈职位但是,想在jquery链中调用该函数。这是我的伪代码。

function togglePanels(f) {
  var customchain;
  if (f == 'n') {
    customchain = $.next();
  } else {
    customchain = $.prev();
  }
  let $next_or_prev_panel = $current_panel
    .parents("div")
    .eq("2")
    .customchain("div")
    .find(".panel");
}

我猜想解决方案会涉及到jQuery库的原型设计?

jquery dynamic prototype-programming
1个回答
1
投票

一个解决方案是使用条件(三元)操作符与一个字符串来设置。customchain 喜欢。

customchain = f == 'n' ? 'next' : 'prev';

然后动态地使用它,像:

let $next_or_prev_panel = $current_panel
    .parents("div")
    .eq("2")[customchain]("div")
    .find(".panel");
© www.soinside.com 2019 - 2024. All rights reserved.