我刚刚看到一个演示,其中使用了这段jquery代码来在悬停时显示和隐藏一个dive,但这不可以只用常规的css完成吗?如果可以用css完成,那么使用javascript有什么优势吗?
$('.comment').hover(function() {
$(this).children('.delete').show();
}, function() {
$(this).children('.delete').hide();
});
我刚刚看到一个演示,其中使用了这段jquery代码来在悬停时显示和隐藏一个dive,但这不可以只用常规的css完成吗?如果可以用css完成,那么使用javascript有什么优势吗?
$('.comment').hover(function() {
$(this).children('.delete').show();
}, function() {
$(this).children('.delete').hide();
});
CSS的:hover伪类在锚标签上可以很好地工作,但IE6无法识别li等元素上的:hover事件。
然而,如果你使用锚标签,你可以在CSS中实现相同的效果:
a.comment .delete { display: none; }
a.comment:hover .delete { display: block; }
你可以使用CSS来完成这个操作,但是IE6只支持锚标签(A)上的:hover伪类,因此这种方法并不常用。
a
标签上使用:hover,但我们目前不知道,因为提问者没有包含任何标记。 - Josh Stodola.hover有更多的功能。如果您提供了超过2个函数,它将循环执行所有函数。 例如:
$('.comment').hover(
function(){$(this).children('.delete.first').show()},
function(){$(this).children('.delete.first').hide()},
function(){$(this).children('.delete.second').show()},
function(){$(this).children('.delete.second').hide()}
);
这将在第一次悬停时显示一个子集,然后隐藏,并在下一次显示另一个子集。
悬停功能还可以在多个元素上工作,并且仅在鼠标离开所有元素(而不仅仅是离开一个并移动到另一个)时触发。
function toggleTab(id)
{
document.getElementById('divEnrollment').style.display='none';
document.getElementById('divSearch').style.display='none';
document.getElementById('divMeeting').style.display='none';
document.getElementById('divBenefit').style.display='none';
document.getElementById('div' + id).style.display='block';
document.getElementById('spnEnrollment').style.color='blue';
document.getElementById('spnSearch').style.color='blue';
document.getElementById('spnMeeting').style.color='blue';
document.getElementById('spnBenefit').style.color='blue';
document.getElementById('spn'+id).style.color = 'red';
}
a
以外的其他元素上的:hover
效果。 - Josh Stodola