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

拟墨画扇 提交于 2019-12-12 18:03:30

问题


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?


回答1:


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/




回答2:


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.




回答3:


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/




回答4:


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/




回答5:


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

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



来源:https://stackoverflow.com/questions/8952512/how-can-i-execute-the-action-only-on-the-div-when-i-have-multiple-div-with-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!