javascript - Jquery $.Ready 函数的最佳实践

标签 javascript jquery asp.net-mvc partial-views

我有很多 MVC3.0 部分 View ,可以在多个页面上重复使用。每个部分 View 都有自己的 JavaScript 和 JQuery $.Ready 函数。

我想知道是否有人可以帮助我了解使用 $.ready 的最佳实践是什么?如下:

  1. 我应该创建 Ready.js,其中包含 $.ready 下应包含的所有事件/函数吗? ?并引用主布局页面。

  2. 或者每个页面都应该有自己的页面。

  3. $.ready 时会发生什么函数执行后找不到引用的元素,因为该页面未使用部分 View 页面。

  4. 组织 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/

相关文章:

asp.net-mvc - 如何使用 Razor 引擎在 mvc5 中显示未排序的图像?

javascript - 仅在 Dartium 中调试我的 Dart 编写的客户端 Web 应用程序是否足够?

javascript - 如何在 javascript 中拆分语言字符串(Wordpress qTranslate)

asp.net-mvc - Visual Studio 2015 bower 智能感知错误

.net - 对于企业级 ASP.Net MVC 应用程序,您建议使用哪种报告工具?

jquery - CSS - 在 div 中居中 <p> 但文本左对齐

javascript - 克隆适用于一个元素,但不适用于整个 div

javascript - 使用规则打印 1-20 之间的数字

javascript - 使用 chrome 浏览器双击时如何删除地址元素突出显示?

jquery - 清除 DIV 中的所有函数