0

javascriptのswitchステートメントは初めてです。

リストアイテムの大きなリストがあります。各アイテムには独自のクラスがあります。リスト項目ごとに、他のことを行う必要があります。

私はこのスイッチコードを作成します:

$('.nav-main li').click(function() {
    var item = this;

    switch (item) {
    case '.menu-intro':
        alert("test");
        break;

    case '.menu-intro-second':
        alert("test2");
        break;
    }
});

しかし、問題は次のとおりです。クラス名を確認するにはどうすればよいですか。nav-mainliアイテムにクラス'menu-intro'がある場合。その後、何かが起こる必要があります。liアイテムにクラス'menu-intro-second'がある場合。別のことよりも起こらなければなりません。

どうすればこれを作ることができますか?

ありがとう!

4

2 に答える 2

1

次のように使用します。

$('.nav-main li').click(function() {
    var item = this;

    switch ($(item).attr('class')) {
      // in case the the class of the element is only menu-intro
      case 'menu-intro':
        alert("test");
        break;
      // in case the the class of the element is only menu-intro-second
      // or in case the class is menu-intro-third
      case 'menu-intro-second':
      case 'menu-intro-third':
        alert("test3");
        break;
      // in case the the classes of the element is menu-intro and active
      case 'menu-intro active':
        alert("test4");
        break;
      // in all other cases...
      default:
        alert("default");
        break;
    }
});
于 2013-02-16T11:37:29.110 に答える
0

単にこれを行う

 var item = this;
    switch($(item).attr('class')) {

    }
于 2013-02-16T11:35:38.610 に答える