jQuery Variable不隐藏

Yah*_*een 1 variables jquery

这是我的代码:

   $('#details').hover(function() {
    var tag = 'div.cds'
    var offset = $(this).position();
    var width = $(tag).outerWidth();
    var height = $(tag).outerHeight();
    $(tag).show();
    $(tag).css('left', offset.left - width + 'px');
    $(tag).css('top', offset.top - height + 'px');
}, function() {
    $(tag).hide();
});
Run Code Online (Sandbox Code Playgroud)

当我"mouseout"时,变量"tag"不会隐藏.

JSFiddle:http://jsfiddle.net/79kLc/

谢谢!

Nea*_*eal 5

学习关于 scope

var tag = 'div.cds'
 $('#details').hover(function() {
    var offset = $(this).position();
    var width = $(tag).outerWidth();
    var height = $(tag).outerHeight();
    $(tag).show();
    $(tag).css('left', offset.left - width + 'px');
    $(tag).css('top', offset.top - height + 'px');
}, function() {
    $(tag).hide();
});
Run Code Online (Sandbox Code Playgroud)

tag变量在第二个函数的范围内不存在.所以我加入tag了全球范围.它现在应该工作了.