你为什么不简单地制作全局变量?(他们可以毫无问题地保存 jQuery 包装集)
// When creating variables holding wrapped sets it is common naming to have
// a $ following it (I find this rather annoying, since you can't easily double click
// on the var, in order to copy/paste it etc, so I always use _ underscore (personal preference)
var _div1 = $('#myFirstDiv'); // you will see a lot of people do div1$
var _div2 = $('#mySecondDiv'); // or div2$ (signifying the jQuery wrapped set)
_div2.hide();
//These could be used within multiple functions etc... No need for namespaces here!
function Whatever () {
console.log(_div1);
_div1.css('background', '#000');
}
$('#linkTrigger').on('click', function () {
Whatever();
_div2.fadeIn();
});
jsFiddle 演示
$.nameSpaceHere
如果您尝试-扩展- jQuery 并添加您自己的某种实用程序函数,则更适合。