传递缓存的 jQuery 引用的推荐方法是什么,例如$domContainer
,var $domContainer = $('#container');
如果函数是在之前和之外定义的,则作为回调传入函数$(document).ready()
?
例子:
<script src="/plugins.js"></script>
在这个可重用函数的外部文件中
function rowAction ( event ) { // how do I get context here?
// how can I access $domTable and $domFilters
// I can access $(event.target) and traverse the dom
// back to $domTable, but $domTable is not defined
// in the external file, although a reference is cached
// in the local scope of $(document).ready();
// likewise, $domTable could be accessed through event.delegateTarget
// however, how can I pass $domFilters, and other vars?
}
在主脚本中
<script src="/scripts.js"></script>
标准文件准备好了
$(document).ready(function(){
// Cached References
var $domFilters = $('#form .filter'), // more than 1 reference returned
$domTable = $('#results'); // 1 reference returned
$domTable.on('click','.menu',rowAction);// reference function in plugins.js
// how do I pass $domFilters to rowAction to avoid dom lookups?
// I could pass $domFilters to a plugin like $(...).plugin({f:$domFilters});
// if it had optional arguments, but if it's not a plugin, what's the
// equivalent way to do it?
});
解决这个问题的方法是使用内联函数来包装回调函数名称吗?
任何指向标准实践的指针也将受到欢迎。