[英]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");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.