在 CoffeeScript/jQuery 中的开关中添加一个 in/contains

Add an in/contains into a switch in CoffeeScript/jQuery

我试图在 URL 包含特定字符串时添加 class。我已经通过直接执行此操作来完成此操作,但这仅适用于 URL 正是条件。

例如,如果 location.search = ?upward_trending=true' 这可以正常工作。但是,如果这是添加到 URL 的附加过滤器,则会中断。

请看下面我的代码。如何将其转换为包含?

switch location.search
    when '?upward_trending=true' then $('a.btn.btn-primary.upward_trending').addClass 'active'

    when '?downward_trending=true' then $('a.btn.btn-primary.downward_trending').addClass 'active'

    else $('a.btn.btn-primary.all_scope').addClass 'active'

请在下面找到解决方案。我改用嵌套的 if 语句和 indexOf 函数:

if (location.search.indexOf('upward_trending')>=0) then $('a.btn.btn-primary.upward_trending').addClass 'active'
else if (location.search.indexOf('downward_trending')>=0)  then $('a.btn.btn-primary.downward_trending').addClass 'active'
else $('a.btn.btn-primary.all_scope').addClass 'active'