How can i execute the action only on the div,when

2019-07-20 04:26发布

How can i execute the action only on the div,when i have multiple div with same class

http://jsfiddle.net/kent93/Qw7bw/

when the mouse enter one div , the other div also will have action , how can i solve this

i want only the div that my mouse goes in take action , not the other, what best solution?

5条回答
一纸荒年 Trace。
2楼-- · 2019-07-20 04:47

Use $(this).children(".fromTopToBottom") instead of $(".fromTopToBottom").

This will select divs of the given class inside the element whose handler you are writing.

查看更多
疯言疯语
3楼-- · 2019-07-20 04:52

What you need is a "current" div concept.

  1. At the beginning of mouseenter handler:

    $(".trbl").removeClass("current"); $(this).addClass("current");

  2. In your case statement, $(".fromTopToBottom").css({...}) -> $(".current .fromTopToBottom").css({...})

For the effect check out http://jsfiddle.net/Qw7bw/7/

查看更多
\"骚年 ilove
4楼-- · 2019-07-20 05:07

very simple, use $(this), for example

$('.mydivclass').mouseover(function(e){
    $(this).css('color','gray'); // Current element
});

If divs are nested then you should use e.stopPropagation() to stop the event bubling.

查看更多
干净又极端
5楼-- · 2019-07-20 05:08

Change the selector of each position to: $(this).children(".class")

for example the code $(".fromTopToBottom") will change to $(this).children(".fromTopToBottom")

Demo: http://jsfiddle.net/Qw7bw/10/

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-07-20 05:11

Use $(this).find(x) rather than $(x) directly when selecting ".fromTopToBottom" and similar classes. This allows jQuery to only select childs of the hovered element http://jsfiddle.net/Qw7bw/6/

查看更多
登录 后发表回答