0

私はstackoverflowが初めてで、coffescriptを使用して.on()関数で私を助けることができるかどうか知りたい.

$('#employee-select')

  .live('click', () ->
    if (!$('#employee-search-panel').is(':visible'))
      employeeLiveSearch()
      $('#employee-search-panel').slideDown()
    else
      $('#employee-search-panel').slideUp()
    end

  )

私が思いついたのは

$(document)

  .on('click', '#employee-select',() ->
    if (!$('#employee-search-panel').is(':visible'))
      employeeLiveSearch()
      $('#employee-search-panel').slideDown()
    else
      $('#employee-search-panel').slideUp()
    end

  )

私の質問は....これは正しい解決策ですか?ドキュメントを実行してから、「クリック?」の横にセレクターを追加します。

よろしく

4

2 に答える 2

0

yes that is correct.. however performancewise it is always better to use the static parent element available in document then the document itself..

 $(document) .on('click', '#employee-select',function(){ //<--- replace document with closest static parentelement
   if (!$('#employee-search-panel').is(':visible'))
     employeeLiveSearch()
     $('#employee-search-panel').slideDown()
  else
     $('#employee-search-panel').slideUp()
  end

})

links to read more about on event

于 2013-03-11T05:38:07.900 に答える
0

This seems correct as you written it, That is the way to delegate the event to the existing parent item in the page.

Although you can try to delegate to the closest existing parent which is available during dom initialized.

like which holds the '#employee-select' (parent of this elem):

$('parent of this').on('click', '#employee-select',() ->

Although (document) is the parent of all elem in the page so that would work absolutely fine.

$(document).on('click', '#employee-select',() ->
于 2013-03-11T05:40:03.967 に答える