【问题标题】:jQuery hover event not firingjQuery悬停事件未触发
【发布时间】:2013-02-19 04:29:37
【问题描述】:

下面给出的代码示例:

(function (window, document) {
$('.rating_title_container').parents('.item_row_def').hover(function() {
          setTimeout(function() {
              system.console('Worked');
          }, 1000);
        });
 })(window, document);  

我对 JS、jQuery 还很陌生。谁能解释我在这里缺少什么? 在http://jsfiddle.net/p7gBy/5/发布代码

HTML

  <table>
    <thead>
      <tr>
        <th class="item_row_def" onclick="sort(3);">
          <table class="col-header">
            <tbody>
              <tr>
                <td>
                  <h2 class="header_title rating_title_container">Rating</h2>
                </td>
              </tr>
            </tbody>
          </table>
        </th>
      </tr>
    </thead>
  </table>

【问题讨论】:

  • 这个代码在你调用它时立即运行,如果它在 head 文档的其余部分还没有准备好,所以它找不到任何元素
  • 你能把它放在小提琴里吗?
  • 你能发布你的 HTML 吗?
  • 已将其添加到 jsfiddle(有问题)

标签: javascript jquery closures


【解决方案1】:

您需要像这样将您的事件处理程序绑定在一个准备好的文档中(用这个替换上面的代码并查看):

$(document).ready(function(){
    $('.rating_title_container').parents('.item_row_def').hover(function() {
      setTimeout(function() {
          system.console('Worked');
      }, 1000);
    });
});

【讨论】:

    【解决方案2】:

    试试这个代码:

    (function (window, document) {
    $('.rating_title_container').parents('.item_row_def').hover(function() {
              setTimeout(function() {
                  alert('Worked');
              }, 1000);
            });
    
    })(window, document); 
    

    检查小提琴http://jsfiddle.net/AXepU/

    【讨论】:

      【解决方案3】:

      尝试以下假设在文档准备好时调用代码:

      jQuery(function (window, document) {
      $('.rating_title_container').parents('.item_row_def').hover(function() {
            setTimeout(function() {
                system.console('Worked');
            }, 1000);
          });
      });
      

      同时你最后超过了});,这会引发错误

      【讨论】:

        【解决方案4】:

        你必须附上那个函数doc ready然后一切都会正常工作:

        $(function(){ // <----------------------------try enclosing within this from here
           (function (window, document) {
              $('.rating_title_container').parents('.item_row_def').hover(function() {
                setTimeout(function() {
                    alert('Worked');
                }, 1000);
              });
           })(window, document); 
        }); //<---------------------------------------- to here.
        

        大部分事件应该写在document ready处理程序中。

        这个:

        $(document).ready(function() {
           // Handler for .ready() called. 
        });
        

        还有这个:

        $(function() {
            // Handler for .ready() called.
        });
        

        都是一样的。第二个是 doc ready 处理程序的较短版本。

        Read More about .ready()处理程序

        【讨论】:

        • 同样的事情你可以select jQuery from the left top panelput a reference in the domdoc-ready
        • 检查这个:jsfiddle.net/p7gBy/7 找到侧边栏的左上角,您必须在其中选择 ready functionframework
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多