jQuery的。需要有关.hover活动的帮助

时间:2011-08-06 12:14:17

标签: jquery

<div id="one" class="hover">Hover there</div>
<div id="one-child" class="box"><div class="region"></div></div>

jQuery(function($) {
$(".hover").hover(function() {
    var targetId = "#" + this.id + "-child"; //Get the id of the new active box
    $(".box").not(targetId).stop(true, true).hide(); //Stop any previous animations and hide the boxes, if they are not the same as the active box.
    $(targetId).fadeIn(); //Show the current, active box
    $(".active").removeClass("active");
    $(this).addClass("active");
}, $.noop); //Provide jQuery's empty function so nothing gets done on mouseleave.
});


jQuery(document).click(function(event) {
    if (!jQuery(event.target).hasClass("box")) { //If the source of the click is a .box, don't do anything.
        jQuery(".box").fadeOut(); //Otherwise fadeOut() the box
        jQuery(".active").removeClass("active");
    }
});

已编辑3

http://kazpost.webcoder.kz→一切正常。 但只留下一个问题:))我在#one-child中放置了另一个div。当我按下#one-child里面的div.region时,div.box就会关闭。

2 个答案:

答案 0 :(得分:0)

更改

if(this.attr('id') == 'box') return;

if($(this).attr('id') == 'box') return;

this.fadeOut('slow');

$(this).fadeOut('slow');

答案 1 :(得分:0)

编辑,最新问题:

jQuery(document).click(function(event) {
    if (jQuery(event.target).closest(".box").length == 0) {
        jQuery(".box").fadeOut();
        jQuery(".act").removeClass("act");
    }
});

可以使用nearest()函数来处理点击,以确定点击的元素是否在.box内。