jQuery向下钻取选择器?

jQuery向下钻取选择器?,jquery,css-selectors,Jquery,Css Selectors,我已经编写了这段代码,它可以在添加类“simplehover”的任何图像上产生滚动悬停效果: //rollover effect on links $(".simplehover").mouseover(function () { $(this).attr("src", $(this).attr("src").replace("-off.", "-on.")); }).mouseout(function () { $(this).attr

我已经编写了这段代码,它可以在添加类“simplehover”的任何图像上产生滚动悬停效果:

    //rollover effect on links 
    $(".simplehover").mouseover(function () {
        $(this).attr("src", $(this).attr("src").replace("-off.", "-on."));
    }).mouseout(function () {
        $(this).attr("src", $(this).attr("src").replace("-on.", "-off."));
    });

<img src="slogan.gif" class="simplehover" />
有人能弄明白吗


您可以使用方法和方法的组合进行重构

$(“a.simplehover-img,img.simplehover”).hover(函数(){
$(this.attr(“src”),$(this.attr(“src”).replace(“-off.”,“-on.”);
},函数(){
$(this.attr(“src”),$(this.attr(“src”).replace(“-on.”,“-off.”);
});

您需要替换url结尾(从开到关,反之亦然),请尝试以下操作:
$(“a.simplehover img,img.simplehover”).hover(函数(){$(this.attr(“src”),函数(i,attr){return attr.replace(“-off.”,“-on.”;},函数(){$(this.attr(“src”,函数(i,attr){return attr.replace(“-on.”,“-off”);})
<a href="#" class="simplehover"> <img src="slogan.gif" /> </a>
$(".simplehover").mouseover(function () {
if ($(this).has('img')) { $(this) = $(this).children(); }
...
$("a.simplehover img, img.simplehover").hover(function () {
    $(this).attr("src", $(this).attr("src").replace("-off.", "-on."));
}, function() {
    $(this).attr("src", $(this).attr("src").replace("-on.", "-off."));
});

<img src="slogan.gif" class="simplehover" />