简体   繁体   English

更改元素背景图片而不是兄弟姐妹

[英]Change elements background image and not siblings

So I have a problem that I do know the solution to, but it would require a ton of repetitive code in the process so I know there must be a simpler way to do this. 所以我有一个我确实知道解决方案的问题,但是在此过程中需要大量重复代码,因此我知道必须有一种更简单的方法来执行此操作。 I have a list of divs that all have a main image and alt image. 我有一个div列表,每个div都有一个主图像和一个alt图像。 The alt images are hidden behind the main images and are supposed to switch with each other when the user hovers over the main image. alt图像隐藏在主图像后面,当用户将鼠标悬停在主图像上时,alt图像应会彼此切换。 Each div has it's own specific ID, but I really don't want to have to write the same script over and over just changing the ID. 每个div都有其自己的特定ID,但我真的不想只需要更改ID就一遍又一遍地编写相同的脚本。 Here's my HTML (simplified but same structure minus fluff): 这是我的HTML(简化但结构相同但没有绒毛):

<section>
    <div id="one">
         <img src="#" class="main" />
         <img src="##" class="alt" style="display: none;" />
    </div>
    <div id="two">
         <img src="#" class="main" />
         <img src="##" class="alt" style="display: none;" />
    </div>
    <div id="three">
         <img src="#" class="main" />
         <img src="##" class="alt" style="display: none;" />
    </div>
</section>

and the jQuery I had written up: 和我写的jQuery:

<script type="text/javascript">
    $(document).ready(function () {
        $('#one img.main').mouseenter(function () {
            $('#one img.main').hide();
            $('#one img.alt').show();
        });
        $('#one img.alt').mouseleave(function () {
            $('#one img.alt').hide();
            $('#one img.main').show();
        });
    });
</script>

I really don't want to repeat for each div since there are 9 in my current page. 我真的不想为每个div重复一次,因为当前页面中有9个。 Any suggestions? 有什么建议么?

SOLVED: 解决了:

jQuery(document).ready(function () {
        //swap out main images on hover
        jQuery('.stores div img.main').mouseenter(function () {
            jQuery(this).hide();
            jQuery(this).next('img.alt').show();
        });
        jQuery('.stores div').mouseleave(function () {
            jQuery('img.alt').hide();
            jQuery('img.main').show();
        });
    });

You should be able to reduce it to: 您应该可以将其减少为:

$(document).ready(function () {
    $('section > div > img.main').mouseenter(function () {
        $(this).hide();
        $(this).next('img.alt').show();
    });
    $('section > div > img.alt').mouseleave(function () {
        $(this).hide();
        $(this).prev('img.main').show();
    });
});

You can use this selector for each first img: 您可以为每个第一个img使用此选择器:

$('section div img:first')

So if you put an id in the section: 因此,如果您在该部分中输入ID:

$('#yourSectionId div img:first')

You need to write scripts mouseenter on .main and mouseleave on the div itself 您需要在.main上编写mouseenter脚本,在div本身上编写mouseleave脚本

<script type="text/javascript">
    $(document).ready(function () {
        $('section > div > img.main').on('mouseenter', function (e) {
            $(this).find('~ .alt').show();
            $(this).hide();
        });
        $('section > div').on('mouseleave', function (e) {
            $(this).find('.alt').hide();
            $(this).find('.main').show();
        });
    });
</script>

I would use: 我会用:

<script>
    $(document).ready(function () {
        $('section > div').on('mouseenter', function () {
            var el = $(this);
            el.find('.main').hide();
            el.find('.alt').show();
        });
        $('section > div').on('mouseleave', function () {
            var el = $(this);
            el.find('.alt').hide();
            el.find('.main').show();
        });
    });
</script>

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

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