这是我的代码:
$('#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();
});
当我“鼠标移开”时,变量“标签”不会隐藏。
JSFiddle在这里:http://jsfiddle.net/79kLc/
谢谢!
最佳答案
了解范围
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();
});
第二个函数的作用域中不存在tag
变量。所以我将 tag
添加到全局范围。现在应该可以工作了。
关于jQuery 变量不隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6100777/