gpt4 book ai didi

javascript - 如何动态查找 div 的 ID 或类?

转载 作者:行者123 更新时间:2023-12-02 19:18:48 27 4
gpt4 key购买 nike

我有一个滚动 div,当您水平滚动时,它会通过 ajax 和 json 从 API 加载数据。

我目前可以使用它,但我遇到的问题是我在单个页面上有多个滚动 div。我需要让 jquery 知道正在滚动的 div 的 ID,以便 ajax 可以使用不同的 API 调用将正确的数据加载到该特定 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...

 $(".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');
}
});
}
}
});

最佳答案

你可以使用

这个

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

之后你就可以使用

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

获取该 div 的 id

关于javascript - 如何动态查找 div 的 ID 或类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12779312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com