【发布时间】:2011-12-08 12:11:49
【问题描述】:
我正在尝试创建一个简单的错误消息,该消息应在页面点击时消失。
这是我的 Jsfiddle:http://jsfiddle.net/ZKgWR/1/
我想在点击页面时隐藏 .super 消息,当消息可见时。
这是我的 jquery:
// show super on click
$('.error').click(function(e) {
e.preventDefault();
var position = $(this).position();
$('.super').css({
display: 'block',
position: 'absolute',
left: position.left + 50,
top: position.top
});
var super = true
});
// If .error clicked then enable the function to remove the super message
if ($('.error').show()) {
$(document).click(function() {
$('.super').hide();
});
}
// If .error clicked then enable the function to remove the super message
if ($('.error').show()) {
$(document).click(function() {
$('.super').hide();
});
}
问题是这部分代码不起作用:
if ($('.error').show()) {
$(document).click(function() {
$('.super').hide();
});
}
单击 .error 时不会附加任何内容,因为当 .super 不可见时,该功能也处于活动状态。
【问题讨论】:
-
您需要稍微澄清一下您的问题。另请参阅stackoverflow.com/questions/178325/…
-
您的代码并不完全清楚,但看起来您需要 :visible,而不是 show()。即 - if ($('.error:visible)){
} api.jquery.com/visible-selector -
问题是当点击带有 .error 类的链接时,div .super 没有显示,因为文档点击功能被触发并且超级 div 被隐藏。隐藏super div的文档点击应该只在super div可见时才有效。
标签: javascript jquery jquery-plugins jquery-selectors