jquery如何只允许对分配的div产生影响,而不是对子元素产生影响

发布于 2024-08-14 05:45:46 字数 742 浏览 7 评论 0原文

我已经对 div 应用了 mouseenter 效果。当您输入 div 时,它会为另一个 div 制作动画。问题是,mouseenter 效果也适用于子 div。如何仅将效果应用于父 div,但为子 div 设置动画?

JS:

$(document).ready(function() {
  $('#slideleft').mouseenter(function() {
    var $lefty = $(this).children();
      $lefty.stop().animate({ 
        left: "0px",
  top: "0px"
             }, 400 );
  });
  $('#slideleft').mouseleave(function() {
    var $lefty = $(this).children();
      $lefty.stop().animate({ 
        left: "-700px",
  top: "200px"
             }, 400 );
  });

});

HTML

<div id="slideleft" class="slide"> 

  <div class="inner">Animate this element's left style property</div> 
</div> 



    </div>

I have applied an mouseenter effect to a div. When you enter the div, it then animates another div. The problem is, the mouseenter effect applies to the children div as well. How do I apply the effect to just the parent div, but animate the children div?

JS:

$(document).ready(function() {
  $('#slideleft').mouseenter(function() {
    var $lefty = $(this).children();
      $lefty.stop().animate({ 
        left: "0px",
  top: "0px"
             }, 400 );
  });
  $('#slideleft').mouseleave(function() {
    var $lefty = $(this).children();
      $lefty.stop().animate({ 
        left: "-700px",
  top: "200px"
             }, 400 );
  });

});

HTML

<div id="slideleft" class="slide"> 

  <div class="inner">Animate this element's left style property</div> 
</div> 



    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-08-21 05:45:46

要仅将动画应用于外部 div,您可以检查事件的目标以确保它与您在 jQuery 方法中选择的 div 匹配。
http://docs.jquery.com/Events/jQuery.Event#event。 target

您的代码如下所示:

$(document).ready(function() {
  $('#slideleft').mouseenter(function(event) {  // added 'event' as param name
        if ( this == event.target )             // ADD THIS LINE
        {
            var $lefty = $(this).children();
            $lefty.stop().animate({
                                    left: "0px",
                                    top: "0px"
                                  }, 400 );
        }
  });

  $('#slideleft').mouseleave(function(event) {
      // uncomment the below IF statement, if you only want to hide
      //  the inner div when mousing out of the outer div.
      // Otherwise the code as is will hide your div if you mouse out of either
      //  the outer or the inner div

      //if ( this == event.target )
      //{
        var $lefty = $(this).children();
        $lefty.stop().animate({
                                left: "-700px",
                                top: "200px"
                              }, 400 );
      //}
  });

});

PS:示例 HTML 中有一个不必要的额外结束 DIV 标记。

To only apply the animation to the outer div, you can check the target of the event to make sure it matches the div that you selected in your jQuery method.
http://docs.jquery.com/Events/jQuery.Event#event.target

Here is what your code would look like:

$(document).ready(function() {
  $('#slideleft').mouseenter(function(event) {  // added 'event' as param name
        if ( this == event.target )             // ADD THIS LINE
        {
            var $lefty = $(this).children();
            $lefty.stop().animate({
                                    left: "0px",
                                    top: "0px"
                                  }, 400 );
        }
  });

  $('#slideleft').mouseleave(function(event) {
      // uncomment the below IF statement, if you only want to hide
      //  the inner div when mousing out of the outer div.
      // Otherwise the code as is will hide your div if you mouse out of either
      //  the outer or the inner div

      //if ( this == event.target )
      //{
        var $lefty = $(this).children();
        $lefty.stop().animate({
                                left: "-700px",
                                top: "200px"
                              }, 400 );
      //}
  });

});

PS: You have an extra closing DIV tag in your example HTML that is not necessary.

风透绣罗衣 2024-08-21 05:45:46

您可以通过不使用 mouseenter/mouseout 事件并使用 jQuery .hover() 来避免孩子们触发 mouseenter/mouseout 事件 函数在你的主 div 上。

jQuery('#slideleft').hover(
        function() {
                // mouse over function
        },
        function() {
                // mouse leave function
        }
);

You can avoid the children firing the mousenter/mouseout events by not using them, and using the jQuery .hover() function on your main div.

jQuery('#slideleft').hover(
        function() {
                // mouse over function
        },
        function() {
                // mouse leave function
        }
);
巾帼英雄 2024-08-21 05:45:46

对子级使用“stopPropagation”,如下所示:

$('#slideleft *').mouseenter(function(event) {        <---- note the asterisk selector
 event.stopPropagation();                        <---- this line
});

这可以防止每个子级的 mouseenter 事件传播到父级。这是它的文档:

http://docs.jquery.com/ Events/jQuery.Event#event.preventDefault.28.29

PS 在 stackoverflow 中保持礼貌非常重要。如果我正确地解释了您上面的一些回复中的一些模棱两可的语气,那么您将来可能需要多考虑一下您的话;-),:-)

Use 'stopPropagation' on the children like so:

$('#slideleft *').mouseenter(function(event) {        <---- note the asterisk selector
 event.stopPropagation();                        <---- this line
});

This prevents the mouseenter event from each child propagating to the parent. Here's the documentation for it:

http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

P.S. It counts a lot to be polite in stackoverflow. If I interpreted a bit of an ambiguous tone in some of your responses above correctly, then you might want to consider your words a little more in the future ;-), :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文