<i id='omghb'><tr id='omghb'><dt id='omghb'><q id='omghb'><span id='omghb'><b id='omghb'><form id='omghb'><ins id='omghb'></ins><ul id='omghb'></ul><sub id='omghb'></sub></form><legend id='omghb'></legend><bdo id='omghb'><pre id='omghb'><center id='omghb'></center></pre></bdo></b><th id='omghb'></th></span></q></dt></tr></i><div id='omghb'><tfoot id='omghb'></tfoot><dl id='omghb'><fieldset id='omghb'></fieldset></dl></div>
          <bdo id='omghb'></bdo><ul id='omghb'></ul>

        <legend id='omghb'><style id='omghb'><dir id='omghb'><q id='omghb'></q></dir></style></legend>

      1. <small id='omghb'></small><noframes id='omghb'>

        <tfoot id='omghb'></tfoot>
      2. 如何使用 `on()` 委托 `hover()` 函数

        时间:2024-04-19
        <tfoot id='jzt9G'></tfoot>
        <legend id='jzt9G'><style id='jzt9G'><dir id='jzt9G'><q id='jzt9G'></q></dir></style></legend>
            <bdo id='jzt9G'></bdo><ul id='jzt9G'></ul>

                    <tbody id='jzt9G'></tbody>
                • <small id='jzt9G'></small><noframes id='jzt9G'>

                • <i id='jzt9G'><tr id='jzt9G'><dt id='jzt9G'><q id='jzt9G'><span id='jzt9G'><b id='jzt9G'><form id='jzt9G'><ins id='jzt9G'></ins><ul id='jzt9G'></ul><sub id='jzt9G'></sub></form><legend id='jzt9G'></legend><bdo id='jzt9G'><pre id='jzt9G'><center id='jzt9G'></center></pre></bdo></b><th id='jzt9G'></th></span></q></dt></tr></i><div id='jzt9G'><tfoot id='jzt9G'></tfoot><dl id='jzt9G'><fieldset id='jzt9G'></fieldset></dl></div>

                • 本文介绍了如何使用 `on()` 委托 `hover()` 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个这样的咖啡代码.

                  I had a coffee code like this.

                    $('.foo').hover 
                      (-> $(this).css "cursor", "pointer"), 
                      (-> $(this).css "cursor", "default")
                  

                  我想将此函数应用于动态附加的 DOM,所以我尝试像这样使用 on() 来委托函数.

                  And I want to apply this function to dynamically appended DOM, so I tried to delegate function by using on() like this.

                  例如,我提到了 这个问题

                    $(document).on 'hover', '.foo', (event) ->
                      $(this).css "cursor", "pointer" if event.type is "mouseover"
                      $(this).css "cursor", "default" if event.type is "mouseout"
                  

                  但是这段代码根本不起作用.

                  But this code doesn't work at all.

                  如何将函数应用于动态添加的元素?

                  How can I apply the function to dynamically added elements?

                  推荐答案

                  我认为您需要取消绑定并添加悬停处理程序.此外,如果要缩进(在这种情况下),则不需要续行.

                  I think you'll need to unbind and add the hover handler. Also, you don't need the line continuations if you're indenting (in this case).

                  $( -> 
                  
                    #debugger 
                  
                    addFooDiv = -> 
                      div = $('<div class="foo">This is another foo div</div>') 
                      $(this).parent().append(div)
                      addFooHandler()
                  
                    onLeaveHandler = -> 
                      $(this).css "cursor", "pointer"  
                      $(this).css "background", "white"
                  
                    onEnterHandler = ->   
                      $(this).css "cursor", "default"  
                      $(this).css "background", "lightblue" # so it's real obvious
                  
                    $('.appendFoo').on('click', addFooDiv)
                  
                    addFooHandler = ->   
                      $(".foo").unbind("hover") 
                      $(".foo").hover(onEnterHandler, onLeaveHandler)
                  
                    addFooHandler()
                  )
                  

                  http://plnkr.co/edit/JbQtqIOhg2AHBCuoHs4N?p=preview

                  给 Sethen Maleno 的建议:https://stackoverflow.com/a/9827114/30946

                  Props to Sethen Maleno: https://stackoverflow.com/a/9827114/30946

                  这篇关于如何使用 `on()` 委托 `hover()` 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 Rails 应用程序部署到 Heroku 时出现语法错误:ExecJS::RuntimeError: SyntaxE 下一篇:JS [ES5] 如何用 setter 和 getter 分配对象?

                  相关文章

                • <small id='NlSTC'></small><noframes id='NlSTC'>

                  <tfoot id='NlSTC'></tfoot>
                      <bdo id='NlSTC'></bdo><ul id='NlSTC'></ul>

                      <i id='NlSTC'><tr id='NlSTC'><dt id='NlSTC'><q id='NlSTC'><span id='NlSTC'><b id='NlSTC'><form id='NlSTC'><ins id='NlSTC'></ins><ul id='NlSTC'></ul><sub id='NlSTC'></sub></form><legend id='NlSTC'></legend><bdo id='NlSTC'><pre id='NlSTC'><center id='NlSTC'></center></pre></bdo></b><th id='NlSTC'></th></span></q></dt></tr></i><div id='NlSTC'><tfoot id='NlSTC'></tfoot><dl id='NlSTC'><fieldset id='NlSTC'></fieldset></dl></div>
                      <legend id='NlSTC'><style id='NlSTC'><dir id='NlSTC'><q id='NlSTC'></q></dir></style></legend>