Using jQuery object understanding
This question already has an answer here:
Second wrapping does nothing as I remember. So if there can be a selector a dom element or a jQuery object you can just wrap it and do not care about what it was.
But if you know it is a jquery object, you shouldn't use wrapping.
当开发人员开发一个函数时,例如在一个jQuery插件中,它可以获得一个DOM元素或jQuery对象或选择器的参数,然后他们使用它:
function x(container) {
container = $(container);
// use container as a jquery object
}
// these both work:
x("#containerId");
x($("#containerId"));
x(document.getElementById("containerId"));
Case 1: Using a jQuery selector to find a dom element and return a jQuery object:
var container = $("#containerId");
// Then use it as:
container.hide();
Case 2: Redundant/mistake (or perhaps if you're unsure if a variable is already a jQuery object but you need it to be): Using a jQuery selector to find a dom element and return a jQuery object then pass it to a new jQuery object:
var container = $("#containerId");
// Then use it as:
$(container).hide();
Case 3: Select a dom element then use it in constructor for a new jQuery object (on which you then call hide() )
var container = document.getElementById("#containerId");
// Then use it as:
$(container).hide();
链接地址: http://www.djcxy.com/p/83932.html
上一篇: 如何知道一个对象是否是jQuery对象
下一篇: 使用jQuery对象的理解