0
我第一次写入jQuery函数。Jquery范围问题
我有这个功能,但我不知道为什么overlay var必须超出click函数的范围。我在内部尝试过,但它不正确。我可以重构这个以使它更好吗?
(function($) {
$.fn.popOver = function() {
// Variable for overlay
var overlay = $("<div id='overlay'></div>");
// Listen for clicks
return this.click(function(e) {
// Prevent the anchor link from loading
e.preventDefault();
// Variable for popover
var popover = $(this).next();
// Append the overlay to the document body
$('body').append(overlay.click(function() {
overlayHide();
}))
//Set the css and fade in our overlay
overlay.show();
popover.fadeIn(150);
// Listen for clicks on elements
popover.find('a').click(function() {
overlayHide();
})
// Hide Overlay function
function overlayHide() {
overlay.remove();
popover.fadeOut(150);
}
})
}
}) (jQuery);
更好的手段更短?这个代码有没有问题? – 2010-12-19 00:15:52
如果你可以缩进你的内部函数以获得更好的可读性,那将会很好。 – deceze 2010-12-19 00:16:50
@deceze没错,缺少缩进让我错过了主要的'click()'处理器:( – alex 2010-12-19 00:18:37