将jquery函数替换为var

replace jquery function with var

本文关键字:var 替换 函数 jquery      更新时间:2023-09-26

我试图用var替换jquery chaned函数,但它不起作用(synthax错误)

Triing something like this:

var foo = 'next()';
$(this).+foo+.show();

有可能以某种方式意识到这一点吗?foo是一个var,在另一个if条件中变为"prev()"

这样做:

var foo = 'next';
$(this)[foo]().show();

通过仅为变量设置方法名称,可以使用[]表示法,并使用该变量检索jQuery对象的next属性。

这相当于…

$(this)['next']().show();

相当于…

$(this).next().show();

试试这个:

var foo = 'next';
$(this)[foo]().show();

我看到了两个选项:

var foo = next; // use the OBJECT here, not a string
$(this).foo().show();
OR
var foo = false;
if (foo)
  $(this).next().show()
else
  $(this).prev().show();