简体   繁体   中英

simulating click on first element of <li>

I tried to simulate a click on the first <li> in the <ul> . Here's the html.

<ul class="product-thumbs clearfix mCustomScrollbar _mCS_1">
    <div class="mCustomScrollBox mCSB_horizontal" id="mCSB_1" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
        <div class="mCSB_container mCS_no_scrollbar" style="position: relative; left: 0px; width: 195px;">
            <li data-full-image="/images/store_logos/e49c796f8740723601503849db95893f6592526a.jpeg" data-large-image="/images/store_logos/e49c796f8740723601503849db95893f6592526a.jpeg" class="active">
                <img src="/images/store_logos/e49c796f8740723601503849db95893f6592526a.jpeg" alt="">
            </li>
            <li data-full-image="/images/store_logos/05838eeaff6014af6206de1cc7a60d7335e61dc1.jpeg" data-large-image="/images/store_logos/05838eeaff6014af6206de1cc7a60d7335e61dc1.jpeg" class="">
                <img src="/images/store_logos/05838eeaff6014af6206de1cc7a60d7335e61dc1.jpeg" alt="">
            </li>
        </div>
        <div class="mCSB_scrollTools" style="position: absolute; display: none;">
            <div class="mCSB_draggerContainer" style="position:relative;">
                <div class="mCSB_dragger" style="position: absolute; left: 0px;">
                    <div class="mCSB_dragger_bar" style="position:relative;"></div>
                </div>
                <div class="mCSB_draggerRail"></div>
            </div>
        </div>
    </div>
</ul>

Here's my jQuery:

 $(".product-thumbs > li:first").trigger('click');

Why is this not working?

try changing

$(".product_thumbs > li:first")

with

$(".product_thumbs li:first")

Because li is not direct child of ul

Here, You cannot use

$(".product-thumbs > li:first")

> is used to filter immediate children. But in your DOM structure, you have

<ul>
  <div>
     <li>
        ...

Hence the issue.

Change it to

$(".product-thumbs li:first").trigger('click')

and it should work.

因为您编写的是product_thumbs而不是product-thumbs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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