0

下面给出的代码示例:

(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>
4

4 回答 4

0

您必须附上该功能,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处理程序的较短版本。

阅读有关 .ready()处理程序的更多信息

于 2013-02-19T04:50:33.017 回答
0

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

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

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

于 2013-02-19T04:33:38.913 回答
0

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

$(document).ready(function(){
    $('.rating_title_container').parents('.item_row_def').hover(function() {
      setTimeout(function() {
          system.console('Worked');
      }, 1000);
    });
});
于 2013-02-19T04:34:30.890 回答
0

试试这个代码:

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

})(window, document); 

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

于 2013-02-19T04:34:48.973 回答