有人可以解释一下= $(); 在jQuery中意味着什么?
我在看一些代码中有以下内容:
// Save an empty jQuery in our cache for now. dialogs[id] = $();
任何人都可以解释它的含义。 我不知道这个概念。 谢谢
更新:
这里有一些代码:
var loadAndShowDialog = function (id, link, url) { var separator = url.indexOf('?') >= 0 ? '&' : '?'; // Save an empty jQuery in our cache for now. dialogs[id] = $(); // Load the dialog with the content=1 QueryString in order to get a PartialView $.get(url + separator + 'content=1') .done(function (content) { dialogs[id] = $('' + content + '') .hide() // Hide the dialog for now so we prevent flicker .appendTo(document.body) .filter('div') // Filter for the div tag only, script tags could surface .dialog({ // Create the jQuery UI dialog title: link.data('dialog-title'), modal: true, resizable: true, draggable: true, width: link.data('dialog-width') || 300 }) .find('form') // Attach logic on forms .submit(formSubmitHandler) .end(); }); };
我仍然看不出第一行的意义。 有人向我解释说它创建了一个空的jquery对象,但我无法理解为什么。
其他答案是正确的; 调用$()
会创建一个空的jQuery对象。
但是,在现有代码的上下文中,似乎并不需要它。 使用$()
初始化的dialogs[id]
变量仅使用其他值重新分配,而不使用其原始值。
然而,需要注意的一点是, 在完成AJAX调用期间 , dialogs[id]
变量随后会被赋予另一个值,这意味着它可以在代码中的其他位置使用,而异步操作$.get
是继续 它可能在函数内部,但它没有使用var
适当的作用域,所以这有点可疑。
从它的外观(以及它的使用方式)来看,我愿意打赌它不是,你可能是正确的, $()
初始化是完全没必要的。
它创建一个空的jQuery对象。 该对象不包含任何实际元素,但它确实添加了所有jQueryfunction。
var $el = $(); // later you can do… $el = $el.add('body'); // same effect as if you'd written `$el = $('body');`
从文档 (你确实检查过文档,对吧?):
返回空集
从jQuery 1.4开始,调用不带参数的
jQuery()
方法会返回一个空的jQuery集(.length
属性为0
)。