繁体   English   中英

jQuery选择器

[英]jquery selectors

我需要在JavaScript中选择一个类并更改其属性之一。 但是由于该类正与不同的div一起使用,因此我需要更改仅悬停在其上的类的属性,而不更改其他悬停的类。 那可能吗?

html结构如下所示:

<div class="home_linkbox_href"><a href=""><div class="home_linkbox">
                    <h1>/h1>
                    <h2></h2>
                </div></a></div>

javascript:

$(".home_linkbox_href a").hover(function(){
    //alert("u did it...");
    $(".home_linkbox_href a .home_linkbox").css("background-color", "#ffe500");

    },
    function(){
        $(".home_linkbox").css("background-color", "#000");
});

有人可以帮忙吗?

我认为您会使用$(this)。

$(".home_linkbox_href a").hover(function(){
    $(this).css("background-color", "#ffe500");
    $(this).attr("href", "http://www.google.com");
    },
    function(){
        $(this).css("background-color", "#000");
});

您可以指定this作为选择,它会发现只有在当前徘徊元素匹配的情况下:

$(".home_linkbox_href a").hover(function(){
    //alert("u did it...");
    $(".home_linkbox", this).css("background-color", "#ffe500");

    },
    function(){
        $(".home_linkbox", this).css("background-color", "#000");
});
$(".home_linkbox_href").find("a").hover(
  function(){
    $(this).siblings(".home_linkbox").css("background-color", "#ffe500");
  },

  function(){
    $(this).siblings(".home_linkbox").css("background-color", "#000");
  }
);

暂无
暂无

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

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