为了避免对jQuery .ready()事件多次调用Asp.Net MVC ScriptBundle中的每个文件,是否可以围绕整个包包装就绪事件?
以下每一个文件都包含$(文档).ready(函数(){ }代码).
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/core.js",
"~/Scripts/application.js",
"~/Scripts/controls.js",
"~/Scripts/custom.js"));我可以创建两个js文件,一个包含$( document ).ready(function() { ),另一个只包含一个结束括号。然后将这些文件添加到现有包的开始和结束,但是有更好的方法吗?
A similar question被要求对文件进行CoffeScript连接,但我无法轻松地将解决方案转换为Asp.Net绑定。
发布于 2015-01-06 15:00:23
如果您正在查看您编写的JavaScript文件(而不是jquery-{version}.js),那么创建一个可以从页面上调用的文件外部公开访问的方法。
var Application = (function () {
// All your code
function init() {
// All your code that you have inside $(document).ready()
}
return {
init: init
};
}());对您的所有文件执行上述操作,然后在任何需要它的页面上,或者在您的_Layout页面中,如果它要在每个页面上运行,则在底部有以下内容
<script type="text/javascript">
$(document).ready(function () {
Application.init();
// etc for the other files.
});
</script>https://stackoverflow.com/questions/27801062
复制相似问题