使用带有布尔状态标志和持续时间的 toggle()

Using toggle() with a boolean state flag and a duration

我正在使用这一行来切换 div:

$(this).closest(".email-settings").find(".notify-email-input").toggle(this.value === "true");

通常您可以将切换速度减慢 .toggle('slow')。 在我的代码中我试过了但是它不起作用。

$(this).closest(".email-settings").find(".notify-email-input").toggle(this.value === "true", 'slow');

我怎样才能使切换变慢?

如果您查看 toggle() 的文档,您会发现接受布尔值的方法签名 接受该布尔值。它不需要任何持续时间或缓动参数。

因此,要按照您的要求进行这项工作,您需要手动检查元素的状态并分别使用 hide()show(),如下所示:

let $emailInput = $(this).closest(".email-settings").find(".notify-email-input");
let method = this.value === "true" ? 'show' : 'hide';
$emailInput[method]('slow');