简体   繁体   English

jquery hover 超过一个元素,突出显示所有元素

[英]jquery hover over one element, highlights all elements

When I hover over one element, all of them are highlighted, here is my code:当我在一个元素上 hover 时,所有元素都突出显示,这是我的代码:

$(document).ready(function(){
            $("p").hover(function(){
            $("p").css("background-color","yellow");
            },function(){
            $("p").css("background-color","transparent");
            });

I was wondering if I could highlight only one at a time instead of adding a class manually to each one我想知道是否可以一次仅突出显示一个,而不是手动为每个添加一个 class

you are targeting every p in the document.您的目标是文档中的每个p you need to restrict it你需要限制它

EDIT: removed first part of answer编辑:删除了答案的第一部分
answer provided by @littletipz is much better. @littletipz 提供的答案要好得多。 second part still stands though...第二部分仍然存在...

OR a CSS only example或仅 CSS 示例

http://jsfiddle.net/bvsTg/ http://jsfiddle.net/bvsTg/

p:hover {
    background-color: yellow;
}

try use $(this)尝试使用$(this)

insted of安装的

 $("p").hover(function(){ $("p").css("background-color","yellow"); },function(){ $("p").css("background-color","transparent"); });

use利用

        $("p").hover(function(){
        $(this).css("background-color","yellow");
        },function(){
        $(this).css("background-color","transparent");
        });

If you dont want to have effects to all elements at once, you can use this stop function:如果您不想一次对所有元素产生影响,可以使用此停止 function:

    $("p").hover(function(){
        $(this).stop().css("background-color","yellow");
    },function(){
        $(this).stop().css("background-color","transparent");
    });

http://api.jquery.com/stop/ http://api.jquery.com/stop/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么jQuery悬停在一个元素上,突出显示所有元素 - why jquery hover over one element, highlights all elements jQuery-将鼠标悬停在一个元素上可以更改两个不同的元素 - jQuery - Hover over one element to change two different elements jQuery通过将一个元素悬停在另一个元素上来使其动画 - jquery animate one element by hover over another jQuery将两个元素悬停在一个元素上 - jQuery treat hover on two elements as one element jQuery hoverintent,在chrome和firefox中工作,当我将鼠标悬停在Internet Explorer中的一个元素上时,所有的悬停功能都会被触发 - jquery hoverintent, works in chrome and firefox, hover when I hover over one element in internet explorer all hover functions are triggered 如果将鼠标悬停在某个元素上,如何在jQuery中选择所有包含并包含它的同级元素? - If I hover over an element, how can I select all sibling elements before it and including it, in jQuery? CSS悬停在两个不同的元素上并影响一个相同的元素 - CSS hover over two different elements and affect one same element 将鼠标悬停在两个元素上可将效果设置为一个元素 - hover over two elements set effect to one element 将元素悬停在其他元素上 - Hover element over other elements 使用jQuery将鼠标悬停在一个元素上并在另一个元素上应用效果 - Using jQuery to Hover over one element and apply the effect in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM