我有很多 MVC3.0 部分 View ,可以在多个页面上重复使用。每个部分 View 都有自己的 JavaScript 和 JQuery $.Ready 函数。
我想知道是否有人可以帮助我了解使用 $.ready
的最佳实践是什么?如下:
我应该创建 Ready.js,其中包含
$.ready
下应包含的所有事件/函数吗? ?并引用主布局页面。或者每个页面都应该有自己的页面。
当
$.ready
时会发生什么函数执行后找不到引用的元素,因为该页面未使用部分 View 页面。组织 JavaScript(即每个页面/小部件/控件的 js)的最佳实践是什么?
最佳答案
使用这个article
$(document).ready(function() { // let's get up in heeya });
Though, it’s very possible that you might have come across a different, more confusing wrapping function.
$(function() { // let's get up in heeya });
Though the latter is somewhat less readable, the two snippets above are identical. Don’t believe me? Just check the jQuery source.
// HANDLE: $(function) // Shortcut for document ready if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); }
rootjQuery is simply a reference to the root jQuery(document). When you pass a selector to the jQuery function, it’ll determine what type of selector you passed: string, tag, id, function, etc. If a function was passed, jQuery will then call its ready() method, and pass your anonymous function as the selector.
关于javascript - Jquery $.Ready 函数的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15124319/