0

I have the following element handling inside a tap event binding.

 $el
   .doThis
   .end()
   .find( "a" ).first().removeClass( someClass );

I need to include a check for condition o.direction == "horizontal" and if the condition is true, I need to toggle instead of remove

However I can't get it to work. Trying like this:

 $el
   .doThis
   .end()
   .find( "a" ).first().toggleClass( someClass , ( o.direction == "horizontal" ? !isCollapse : event ) );

Toggles the class correctly if o.direction == "horizontal", but if it's not, I cannot remove instead of toggle.

Question:
Is there a way to check for a condition and perform a toggle if condition is met and a removeClass if not in a chained jquery statement?

4

3 に答える 3

1
$el
   .doThis
   .end()
   .find( "a" ).first()[o.direction == 'horizontal' ? 'toggleClass':'removeClass']( someClass )
于 2012-09-11T19:13:02.190 に答える
1

eventオブジェクトは条件で使用されるべきではありませんelsefalse代わりに使用する必要があります:

.toggleClass( someClass , ( o.direction == "horizontal" ? !isCollapse : false ) )

これは、の場合o.direction == "horizontal"、cssクラスがisCollapse値に応じて切り替えられることを意味します。それ以外の場合、cssクラスは削除されます。

于 2012-09-11T19:13:15.163 に答える
1

実行時の要素の状態がわからず、その状態の変数を持つことが非常に難しいという問題を解決するために、通常はデータ属性を使用し、コードでそれらを管理します。

<div data-direction="horizontal">

その後

if(element.getAttribute("data-direction") == "horizontal")
{
 //TODO: remove or toggle
}

状態を管理する

element.setAttribute("data-direction","state");
于 2012-09-11T19:08:22.797 に答える