1

ユーザーがアイテムをクリックして拡張機能を表示するメガドロップダウンメニューを実行しています。アイテムの onclick で 2 つの結果が必要です。

  1. 表示される拡張子
  2. メニュー項目 (クリックされたもの) のために変更する背景色

しかし、私は苦労しており、これらの結果は両方とも onclick イベントが発生する前に発生します。

他のすべてのものはこれまでのところ良好です 以下は私のコードです

//call the showMenu function to toggle menu items display   
showMenu("#market", "#marketDrop");
showMenu("#jobs", "#jobsDrop");
showMenu("#career", "#careerDrop");
showMenu("#tech", "#techDrop");
showMenu("#estate", "#estateDrop");
showMenu("#fair", "#fairDrop");
showMenu("#leisure", "#leisureDrop");
showMenu("#exclusive", "#exclusiveDrop");

//the showMenu function
function showMenu(listItem, dropDown){
  //first hide the main drop down containers
  $(dropDown).hide();   
  $(listItem).click(function() {
    $(dropDown).fadeToggle();
  });

  $(listItem).click().css("background-color", "#000");      
}
4

1 に答える 1

3

最後の行は、クリック ハンドラーをバインドするのではなく、すぐにクリックをトリガーしています。

これを試して:

function showMenu(listItem, dropDown){
    //first hide the main drop down containers
    $(dropDown).hide();   

    // register the click handler
    $(listItem).click(function() {
        $(dropDown).fadeToggle();
        $(this).css("background-color", "#000");
    });
}

注: 最新の jQuery.on('click', ...)では、.click(...). .click引数リストに応じて、トリガー ハンドラーとバインド ハンドラーの両方に使用されることによる混乱を避けるのに役立ちます。まだ古いバージョンの jQuery を使用している場合に備えて、それを使用するようにコードを変更していません。

于 2012-10-30T16:14:40.823 に答える