如何动态找出div的ID或类? [英] How do I dynamically find out what the ID or class of a div?

查看:125
本文介绍了如何动态找出div的ID或类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动div,它通过ajax&您在水平滚动时从API中获取json.

I have a scrolling div that loading in data via ajax & json from an API as you horizontally scroll.

我目前可以使用它,但问题是我在单个页面上有多个滚动div.我需要让jquery知道正在滚动的div的ID,以便ajax可以使用其他API调用将正确的数据加载到该特定div.

I currently have it working but the problem I have is that I have multiple scrolling divs on a single page. I need to let jquery know the ID of the div that is being scrolled so that the ajax can use a different API call to load in the correct data to that specific div.

这是HTML代码:

<div id="ip-technology" class="scroll-box">
<h2>Latest Videogames Titles</h2>
    <a class="view" href="technology.html">See all</a>
    <ul class="scroll-horizontal mCustomScrollbar _mCS_3">
    <div class="mCustomScrollBox mCSB_horizontal" id="mCSB_3" style="position:relative; height:100%; overflow:hidden; max-width:100%;"><div class="mCSB_container" style="position: relative; left: 0px; width: 6721px; ">  
    <li class="product-Magazine">
        <a href="#">
            <span class="store-badge"></span>
        <img src="cover.jpg" width="124" height="166">
    </li>
           </div>

    <div class="mCSB_scrollTools" style="position: absolute; display: block; ">
    <div class="mCSB_draggerContainer" style="position:relative;">
    <div class="mCSB_dragger ui-draggable" style="position: absolute; width: 149px; ">
    <div class="mCSB_dragger_bar" style="position:relative;">
    </div></div><div class="mCSB_draggerRail"></div>
    </div></div></div>
</ul>

这是jquery/ajax ...

And here is the jquery/ajax...

 $(".scroll-horizontal").mCustomScrollbar({
        horizontalScroll:true,
        scrollEasing:"easeOutBack",
        advanced:{
            autoExpandHorizontalScroll: true
        },
        callbacks:{
            onTotalScrollOffset: 30,
            onTotalScroll: function(){                  

             var url = 'http://www.URL-TO-API.com' //Needs to somehow pass in the div id

              $.ajax({
                    type: 'GET',
                    url: url,
                    async: true,
                    jsonpCallback: 'callback',
                    dataType: 'jsonp',
                    success: function(data)
                    {

                         $.each(data.products, function(key, val)
                         {
                                if (loadNumber <= 4) {
                                     $('li.loading').before('<li class="product-ebook"><a href="product.html"><span class="store-badge"></span><img src="'+val.image+'" width="124" height="166" /><h3>'+val.title+'</h3></a></li>');
                                };
                                if (loadNumber >= 4) {
                                     $('li.loading').hide();
                                };
                         });

                          $('h3').html(function(index, currentHtml) {
                              return currentHtml.replace('Issue', '<span>Issue')
                                                .replace('Vol', '<span>Vol');
                              $(this).apppend("</span>");
                          });
                          $('.scroll-horizontal').mCustomScrollbar("update");


                    },
                    error: function() {
                        console.log('failed');
                    }
                    });
            }
        }
    });

推荐答案

您可以使用

this

这将为您提供正在滚动的特定div

which will give you particular div that is being scrolled

然后您可以使用

$(this).find('.mCSB_horizontal')attr('id');

获取该div的ID

这篇关于如何动态找出div的ID或类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆