简体   繁体   English

选择器CSS JQuery

[英]Selector CSS JQuery

Is it normal that 正常吗

$("#foo a:hover")

does not select anything? 没有选择任何东西?

I can't use hover cause I need this selector to do that : 我不能使用悬停,因为我需要这个选择器来做到这一点:

 $("#foo a:hover").css("color", mycolor);

So I wonder if something like 所以我想知道是否

 $("#foo").css("a:hover color", mycolor);

exist? 存在?

Et What about visited (pseudo class)? 等等访问过的(伪类)?

Yes, that is completely normal. 是的,那是完全正常的。 jQuery doesn't have a :hover pseudo class. jQuery没有:hover伪类。

You can use the hover method to bind two event handlers that add and remove the style: 您可以使用hover方法绑定两个添加和删除样式的事件处理程序:

$("#foo a").hover(function(){
  $(this).css("color", mycolor);
},function(){
  $(this).css("color", "");
});

是的,使用.hover()

Edited: 编辑:

$("#foo a").hover(function(){ 
   $(this).css('color','mycolor');
});

When nothing is currently hovered, yes. 当前没有任何内容时,可以。 Something would need to concurrently be in the hover state. 某些东西需要同时处于悬停状态。

Yes, refer to the jQuery selector documentation for a list of valid selectors. 是的,请参阅jQuery选择器文档以获取有效选择器的列表。

Instead you should be using the jquery .hover() with $("#foo a") 相反,您应该将jquery .hover()$("#foo a")

http://api.jquery.com/hover/ http://api.jquery.com/hover/

Yes because that selector will have fired BEFORE you hover anything. 是的,因为在您悬停任何内容之前,该选择器已被触发。 For example, if you were to just do: 例如,如果您要这样做:

$('#foo')

And then later on in the script append() foo it will not select anything because it didn't exist. 然后,在脚本append() foo中,它不会选择任何内容,因为它不存在。

If you DO want to do something on hover you can do .hover(function(){},function(){}) 如果您想在悬停上做某事,可以执行.hover(function(){},function(){})

您在标记处于悬浮状态时选择标记(“:hover”是伪类),因此您需要将鼠标悬停在标记上并同时运行该脚本才能成功选择元素。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM