2017-08-08 6 views
0

展開/折りたたみボタンを作成し、別のdivに割り当てる必要があります。ここに私のコードは次のとおりです。異なるdivに同じボタンを割り当て、クリックでtypescriptを個別に管理する方法

let expandButtonHTML = require("!text-loader!./button.html"); 
... 
let expandButton = frame.contents().find(".expandMenuArrow"); 
let topMenu = frame.contents().find(".efdRLHover"); 
let bottomMenu = frame.contents().find(".pbHeader"); 

topMenu.prepend(expandButtonHTML); 
bottomMenu.prepend(expandButtonHTML); 

let div = [topMenu, bottomMenu]; 

div.forEach(element => { 
expandButton.click(function() { 
       element.slideToggle("fast"); 

       if (expandButton.hasClass('fa fa-caret-down')) { 
        expandButton.removeClass('fa fa-caret-down'); 
        expandButton.toggleClass('fa fa-caret-right'); 
       } 
       else { 
        expandButton.removeClass('fa fa-caret-right'); 
        expandButton.toggleClass('fa fa-caret-down'); 
       } 
      }); 
}); 

私は、foreachループは、アレイ内のすべての要素を通過し、それらの上にexpandButton機能をやってますが、どのように私はそれを構築することができているので、特定のdivは、特定の機能を実行クリックすることを知っていますそれ?トップメニューの

クリックすると、トップメニューを拡大し、ように...

答えて

1

は、私はあなたの代わりに、ループの外にそれを発見する、ループ内トップメニューとbottomMenuの両方で別々にexpandButtonを見つける必要があるかもしれないと思います。多分これが助けになるでしょうか?

div.forEach(element => { 
     let expandButton = $(".expandMenuArrow", element); 
     expandButton.click(function() { 
      element.slideToggle("fast"); 

      if (expandButton.hasClass('fa fa-caret-down')) { 
       expandButton.removeClass('fa fa-caret-down'); 
       expandButton.toggleClass('fa fa-caret-right'); 
      } 
      else { 
       expandButton.removeClass('fa fa-caret-right'); 
       expandButton.toggleClass('fa fa-caret-down'); 
      } 
     }); 
    }); 
+0

ありがとうございます!上記の私の問題を解決した - 私はループ内ではなく、外部でexpandButtonを定義しなければならないと言ったように。 –

関連する問題